Zhenning Guan
2010-Sep-04 13:08 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
def process! #transaction block bankbook = self.bankbooks.build bankbook = user.bank.bankbooks.build bankbook.withdraw #end end it "should process the withdrawal request" do #something here omit withdrawal.process! @ning.bankbooks.last.price.should == BigDecimal(''1.00'') @ning.bankbooks.last.action.should == ''withdraw'' end bank will withdraw money and bankbook create a record for this withdraw. after a while, I think bankbook should create withdraw item after bank withdraw. so I change the code like following code. def process! #transaction block bank.withdraw #end end # bank model def xxxx bankbook = bankbooks.build bankbook.withdraw end the ''should process the withdrawal request'' it''s true. but after the change we got two bankbook items. so what''s the correct way to create this test? -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Sep-04 13:20 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
On Sep 4, 2010, at 8:08 AM, Zhenning Guan wrote:> def process! > #transaction block > bankbook = self.bankbooks.build > bankbook = user.bank.bankbooks.build > bankbook.withdraw > #end > end > > it "should process the withdrawal request" do > #something here omit > withdrawal.process! > @ning.bankbooks.last.price.should == BigDecimal(''1.00'') > @ning.bankbooks.last.action.should == ''withdraw'' > end > > bank will withdraw money and bankbook create a record for this withdraw. > after a while, I think bankbook should create withdraw item after bank > withdraw. > so I change the code like following code. > def process! > #transaction block > bank.withdraw > #end > end > > # bank model > def xxxx > bankbook = bankbooks.build > bankbook.withdraw > end > > the ''should process the withdrawal request'' it''s true. but after the > change we got two bankbook items. so what''s the correct way to create > this test?I''m having a hard time understanding what you''re trying to do here. Can you please post the full before and after code and spec listing in a gist or pastie? http://gist.github.com http://pastie.org
Zhenning Guan
2010-Sep-07 01:37 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
David Chelimsky wrote:> I''m having a hard time understanding what you''re trying to do here. Can > you please post the full before and after code and spec listing in a > gist or pastie? > > http://gist.github.com > http://pastie.orgthe before just mock a user. and user request to withdraw money from he''s bank account, after that, we record his action like ''david withdraw $10'' in bankbook. the problem above is I expect one bankbook ''david withdraw $10'' but create two ''david withdraw $10''.in database, just because the I use last api to receive last record. @ning.bankbooks.last.action.should == ''withdraw'' I guess I should do a count check like lambda{ withdrawal.process!}.should change {Bankbook.count}.by(1) -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Sep-07 02:11 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
On Sep 6, 2010, at 8:37 PM, Zhenning Guan wrote:> David Chelimsky wrote: >> I''m having a hard time understanding what you''re trying to do here. Can >> you please post the full before and after code and spec listing in a >> gist or pastie? >> >> http://gist.github.com >> http://pastie.org > > the before just mock a user. and user request to withdraw money from > he''s bank account, after that, we record his action like ''david withdraw > $10'' in bankbook. > the problem above is I expect one bankbook ''david withdraw $10'' but > create two ''david withdraw $10''.in database, just because the I use last > api to receive last record. > @ning.bankbooks.last.action.should == ''withdraw'' > > I guess I should do a count check like > > lambda{ withdrawal.process!}.should change {Bankbook.count}.by(1)By "before and after" I meant what the code looked like before you made the change you want to make, and what it looked like after the change.
Zhenning Guan
2010-Sep-07 02:28 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
David Chelimsky wrote:> By "before and after" I meant what the code looked like before you made > the change you want to make, and what it looked like after the change.sorry I misunderstood your word. def process! #transaction block bankbook = self.bankbooks.build bankbook = user.bank.bankbooks.build bankbook.withdraw #end end that''s the before code.and I change it to. def process! #transaction block bank.withdraw #end end because I have been miss the create banbooks in bank model withdraw method. code like -- # bank model def xxxx bankbook = bankbooks.build bankbook.withdraw end -- hope I clear my thought. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Sep-07 10:44 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
On Sep 6, 2010, at 9:28 PM, Zhenning Guan wrote:> David Chelimsky wrote: >> By "before and after" I meant what the code looked like before you made >> the change you want to make, and what it looked like after the change. > > > sorry I misunderstood your word. > > > > def process! > #transaction block > bankbook = self.bankbooks.build > bankbook = user.bank.bankbooks.build > bankbook.withdraw > #end > end > > that''s the before code.and I change it to. > > def process! > #transaction block > bank.withdraw > #end > end > > because I have been miss the create banbooks in bank model withdraw > method. > code like > -- > # bank model > def xxxxIs this withdraw instead of xxxx?> bankbook = bankbooks.build > bankbook.withdraw > end > -- > > hope I clear my thought.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?
Zhenning Guan
2010-Sep-07 12:18 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
> 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. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Sep-07 13:20 UTC
[rspec-users] how to wrtie this test?(test true but change when refactor)
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) HTH, David