[rspec-users] Spec''ing a block

Matt Wynne
Mon, 05 Dec 2011 14:39:39 -0000

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