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?

On Sep 4, 2010, at 8:08 AM, Zhenning G. 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

David C. 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)

On Sep 6, 2010, at 8:37 PM, Zhenning G. wrote:

$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.

David C. 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.

On Sep 6, 2010, at 9:28 PM, Zhenning G. wrote:

#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

Is 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?

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.

On Sep 7, 2010, at 7:18 AM, Zhenning G. wrote:

#end
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs