Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>tim_yates</strong> wrote:</p> <pre><code>def mock = new MockFor(File) mock.demand.eachFile { [new File("script1.sql"), new File("script2.syn"), new File("script3.grt")].each { (it) } } mock.use { new File('.').eachFile {file -&gt; println "$file" } </code></pre> <p>He was right. As I said, I didn't use Groovy that long(neither Spock).</p> <p>"I'm guessing thats because there isnt direct usage of File.eachFile() in the code(it doesn't pass the mock to the method, it expects the mock to be instantiated in the "use" block)?"</p> <p>The code in the test looks like this:</p> <pre><code>when: mock.use { folderValidator.validate(new File(".")) } then: folderValidator.listMissingFiles.size == 3 </code></pre> <p>The solution was to simply instantiate the mocked class inside the use block. I thought that the mock is instantiated and would be passed in the method - the way Mockito works.</p> <p>Mockito:</p> <pre><code>File pathScripts; pathScripts = mock(File.class); when(pathScripts.list()).thenReturn(listFiles); someObject.someTestingMethod(pathScripts) </code></pre> <p>The proxyInstance way would be to mock the class, <strong>BUT WITH PROPER CONSTRUCTOR ARGUMENTS</strong>!</p> <pre><code>def mock = new MockFor(File) mock.demand.eachFile {func -&gt; [new File("script1.sql"), new File("script2.syn"), new File("script3.grt")].each { func(it) } } def mockProxy = mock.proxyInstance(".") mockProxy.eachFile {file -&gt; println "$file" } </code></pre> <p>And, the biggest mistake was the missing closure in my mocked method:</p> <pre><code>mockFile.demand.eachFile { **??** [new File("script1.sql"), new File("script2.syn"), new File("script3.grt")].each {**?**(it)}} </code></pre> <p>So, thank you, <strong>tim_yates</strong>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload