[rspec-users] given_it

Yurii Rashkovskii yrashk at gmail.com
Fri Sep 21 16:26:55 EDT 2007


Ok, let me try to explain. I have some controllers that are doing  
some kind of "main" action and some additional ones. These actions  
are belong to different aspects of logic and thus
I'd prefer to test them separately to have clean and nice code that  
can be easily modified.

Then, what's better to read?

it "should do main action" do
   do_main_action
end

it "should set some value in session if there was no another specific  
value in session before" do
   session[:a] = 1
   do_main_action
   session[:b].should == b
end

def do_main_action
       post :create, ...
end

or

it "should do main action" do
   post :create, ...
end

it "should set some value in session if there was no another specific  
value in session before" do
   session[:a] = 1
   given_it "should do main action"
   session[:b].should == b
end

I personally like second one.

P.S. Examples above are surely artificial.



On Sep 21, 2007, at 11:11 PM, lancecarlson at gmail.com wrote:

> This concept violates oo if you want to test whether or not r  
> method calls another method you would do that in the test and label  
> it occordingly. What you are defining is such a narrow use case I  
> think it would be abused
> Sent via BlackBerry from T-Mobile
>
> -----Original Message-----
> From: "David Chelimsky" <dchelimsky at gmail.com>
>
> Date: Fri, 21 Sep 2007 22:03:30
> To:rspec-users <rspec-users at rubyforge.org>
> Subject: Re: [rspec-users] given_it
>
>
> On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote:
>> Sure! But they are not the same stuff. What I was looking for is
>>
>> it "should do #1" do
>>   ...
>> end
>> it "should do X after #1" do
>>   given_it "should do #1"
>>   should do_x
>> end
>> it "should do Y after #1" do
>>   given_it "should do #1"
>>   should do_y
>> end
>> it "should do Z after #1 Y" do
>>   given_it "should do Y after #1"
>>   should do_z
>> end
>
> Sorry mate - that just seems like endless confusion - the examples
> should never rely on each other that way - different animal from
> scenarios that involve steps.
>
>>
>> Or am I missing somethiing?
>>
>>
>> On Sep 21, 2007, at 10:49 PM, David Chelimsky wrote:
>>
>>> Have you seen shared behaviours?
>>>
>>> On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote:
>>>> Hello,
>>>>
>>>> Just decided to check whether I am doing something that makes sense
>>>> or not. I was thinking about how cool would it be to re-use  
>>>> examples
>>>> (just like we reuse story scenarios with GivenScenario). I was not
>>>> sure if this possibility already exists in rspec (and, honestly,  
>>>> was
>>>> lazy to check), so I have created this helper:
>>>>
>>>> def given_it(name)
>>>>    example_definition = behaviour.example_definitions.find{|i|
>>>> i.description == name }
>>>>    instance_eval(&example_definition.example_block)
>>>> end
>>>>
>>>> so it is possible to write things like
>>>>
>>>> it "should do something after another action" do
>>>>    given_it "should successfully do another action"
>>>>    do_something.should be_fine
>>>> end
>>>>
>>>> Is there anything already in rspec that allows me to do the same
>>>> stuff? Or was it a bad idea at all?
>>>>
>>>> Yurii.
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list