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

Justin Ko jko170 at gmail.com
Fri Aug 27 02:36:47 EDT 2010


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.

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


More information about the rspec-users mailing list