[rspec-users] mocking the shell command (Kernel module)

Scott Taylor mailing_lists at railsnewbie.com
Fri Sep 12 23:13:55 EDT 2008


On Sep 12, 2008, at 9:29 AM, Matt Wynne wrote:

> On 12 Sep 2008, at 14:12, Joaquin Rivera Padron wrote:
>
>> what is the best (or any) way of mocking the running of shell  
>> commands?
>>
>> e.g.
>> code like the following:
>>
>> def method
>>   %{ ls }
>> end
>>
>> spec:
>>
>> it "should list the directory contents"
>>   shell = mock(Object) # %{} lives in Kernel module and its sugar  
>> for `
>>   shell.should_receive(:`).with(:ls)
>> end
>>
>> sorry about latter one, thanks in advance
>> joaquin
>
> I suggest you put a 'seam' between your code and the call the Kernel.

That sounds like a good idea.  You can also Kernel#` directly (instead  
of `foo` call Kernel.send(:`, "foo").  This allows you to stub out  
Kernel#`.

Scott



More information about the rspec-users mailing list