[rspec-users] there should be one test or two?

David Chelimsky dchelimsky at gmail.com
Fri Aug 27 18:12:16 EDT 2010


On Aug 27, 2010, at 1:36 AM, Justin Ko wrote:

> The "best practice" in your situation is to have two specs like so:
> 
> describe "#deposit" do
>  it 'adds $10 to a savings account' do
>    user.bank.deposit(10)
>    user.bank.deposit.saving.should == 10
>  end
> 
>  it 'creates a deposit record' do
>    user.bank.deposit(10)
>    user.deposit_record.should == #something.
>  end
> end
> 
> There are two downsides to having multiple expectations (should's) in
> one example/spec:
> 
> 1.) If the first expectation fails, the second one will not run.
>    false.should be_true # fails
>    false.should be_false # never runs
> 
> 2.) It is much more simple for the description of the example/spec to
> specify one expected behaviour. If you did something like this:
>    it "deposits $10 to a savings account and adds a new record"
>    You would have to remember that you're checking for TWO
> behaviours, which is not as clear and direct as one.

There's also an upside to separating the examples (it's not all avoiding negatives), which is that you express the spec more accurately. Consider the output (slightly rephrased):

Account
  #deposit
    increases the balance
    creates a deposit record

Now it _looks like_ a spec.

HTH,
David

> 
> Hope this helps.
> 
> On Aug 27, 1:43 am, Zhenning Guan <li... at ruby-forum.com> wrote:
>> in real world, when user deposit money into their bank, bank have money
>> and can check deposit record. so what would it be in rspec?
>> 
>> it 'should be deposit $10'
>>   user.bank.deposit(10)
>>   user.bank.deposit.saving.should == 10
>> end
>> 
>> about test , should be deposit $10 is clear? maybe 'should deposit $10
>> success'?
>> 
>> and when deposit, we should have a deposit record. added another test?
>> 
>> it 'should be a deposit record when deposit $10'
>>   user.bank.deposit(10)
>>   user.deposit_record.should == #something.
>> end
>> 
>> or just mixed it in one test?
>> 
>> it 'should be deposit $10'
>>   user.bank.deposit(10)
>>   user.bank.deposit.saving.should == 10
>>   user.deposit_record.should == #something.
>> end
>> --
>> Posted viahttp://www.ruby-forum.com/.
>> _______________________________________________
>> rspec-users mailing list
>> rspec-us... at rubyforge.orghttp://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