[rspec-users] testing an action which requires picture
Nick Hoffman
nick at deadorange.com
Thu Nov 20 12:30:01 EST 2008
On 2008-11-18, at 01:53, Mano ah wrote:
> Nick Hoffman wrote:
>> On 2008-11-14, at 06:29, Mano ah wrote:
>>> image = Image.new
>>>
>>> image.blob= params[:image][:blob]
>>>
>>> image.save_picture
>>>
>>> end
>>> --
>>
>> Hi Mano. It doesn't really matter whether or not your "picture" model
>> interacts with a database. Simply use mocks and stubs when speccing
>> #scan , and you'll be good. For example:
>>
>> describe '#scan' do
>> describe 'receives a POST request' do
>> before :each do
>> # mock an Image instance
>> # stub Image#new
>> end
>>
>> it 'should create a new Image'
>> it "should set the new image's 'blob' attribute"
>> it 'should save the new image'
>> end
>>
>> describe 'receives a non-POST request' do
>> # stuff here
>> end
>> end
>>
>> Obviously you have to fill in the contents of those #it blocks, but
>> that's really all you need.
>> -Nick
>
Hi Mano.
> All the above specification passed.
Um, that's because those calls to #it were meant as a guide for you,
and don't actually contain any real tests. You need to write those
yourself.
> Now I want to test the return value. I mean
>
>
> def scan
> #-------
>
> if request.post?
>
> image = Image.new
>
> image.blob= params[:image][:blob]
>
> if image.save_picture
>
> @code = returns a blog image value
> end
>
> end
>
>
> What is the rspec code to test the return value and make it pass
The code above is trivial to spec if you understand RSpec. It sounds
like you need to learn more about RSpec and behaviour-driven
development. I recommend searching Google for a couple of articles/
guides, as well as reading what David Chelimsky, Dan North, and other
experts have written on their blogs/sites.
Cheers,
Nick
More information about the rspec-users
mailing list