[rspec-users] how to wrtie this test?(test true but change when refactor)

David Chelimsky dchelimsky at gmail.com
Tue Sep 7 09:20:42 EDT 2010

On Sep 7, 2010, at 7:18 AM, Zhenning Guan wrote:

>> In your original post you said you were getting two bankbook items after 
>> the change. Do you want one or two? And where is the 2nd one coming 
>> from?
> I always want one . after change the code I got two. and the original 
> test doesn't fail.
> def process!
>  #transaction block
>    bank.withdraw
>    bankbook = user.bank.bankbooks.build
>    bankbook.withdraw
>  #end
> end
> #change version
> def process!
>  #transaction block
>   bank.withdraw
>  #end
> end
> so obviously, bankbook = user.bank.bankbooks.build create a bankbook 
> record. and bank.withdraw jsut about decrease money.
> after change. bank.withdraw Model#withdraw the withdraw method will 
> create bankbook record. so we just don't need create bankbook in 
> process! but my test is
>    @ning.bankbooks.last.price.should == BigDecimal('1.00')
>    @ning.bankbooks.last.action.should == 'withdraw'
> it doesn't care how many bankbook record was created.

So are you asking how to specify that only one record gets created? If so, your suggestion a few posts back is good:

lambda{ withdrawal.process! }.should change {Bankbook.count}.by(1)


More information about the rspec-users mailing list