[rspec-users] Spec''ing a block

David Chelimsky
Mon, 05 Dec 2011 17:21:34 -0000

On Mon, Dec 5, 2011 at 9:13 AM, Morten M?ller Riis
<mortenmoellerriis at gmail.com> wrote:
> Hi Matt
>
> Thank you for your reply!
>
> I mock things like FileUtils for the following reasons: to avoid testing
> FileUtils which I reckon has its own test suite and to avoid doing
> potentially harmful things locally or having specs fail because of
> insufficient permissions etc. I know the last bit could be worked out, but
> for things like #rm_r I do like just to mock them.

Have you looked at FakeFS? It is faster than actually working on the
filesystem, and it avoids problems like accidentally deleting stuff.

> Please let me know if this is not best practice. I know one can
"over-mock"
> a spec suite, but I generally tend to mock things that are
tested/spec''ed
> themselves.
>
> Mvh
> Morten M?ller Riis
>
>
>
> On Dec 5, 2011, at 3:39 PM, Matt Wynne wrote:
>
>
> On 5 Dec 2011, at 12:04, Morten M?ller Riis wrote:
>
> Hi folks
>
>
> How would you spec something like this:
>
>
> ??as_user username do
>
> ????FileUtils.chmod_R 0755, "#{directory}/*"
>
> ??end
>
>
> Where as_user fires off a new process (and set uid to username).
>
>
> It seems that this won''t catch FileUtils.chmod_R:
>
>
> FileUtils.should_receive(:chmod_R).with(0755,
"#{@domain.directory}/*")
>
>
> I guess that is because it is passed in the block and fired off in a
> seperate process (Process.fork).
>
>
>
> It depends on what you want to prove.
>
> If you want to prove that this bit of code will set the actual flags on the
> actual file, then why not let it do it, and then check that the file ends
up
> how you want it to? If it''s happening in a forked process,
you''ll need to
> wait for the process to close to be sure it''s done, but otherwise
that
> should be quite straightforward, and will give you confidence that the
whole
> thing is working.
>
> Otherwise, you need to put a layer around the whole detail of forking and
> running the FileUtils command, and put your mock assertion against that
> layer. Right now you''re trying to introduce your mock into the
stuff that
> happens in the forked process, which isn''t going to work.
>
> cheers,
> Matt
>
> --
> Freelance programmer & coach
> Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak
> Helles?y)
> Founder, http://relishapp.com
> +44(0)7974430184 | http://twitter.com/mattwynne