未返回存根方法返回值
我已经存根了一个方法并要求它返回一个特定的值。然而,当运行单元测试时,真实方法被调用并返回真实值生成值。
即我对 get_requisition_number 方法进行了存根处理以返回值 1
,但是在执行单元测试时它返回值 2011031100001
单元测试代码:
it "should have a unique requisition number when saved" do
Requisition.stub(:get_requisition_number).and_return("1")
req1 = Requisition.new
req1.save
req2 = Requisition.new
lambda { req2.save! }.should raise_error(ActiveRecord::ActiveRecordError)
end
get_requisition_number 方法在 save 方法时被调用被执行。我假设从存根它应该返回1
。但是,它返回一个特定于日期的值,例如 2011031100001
,这意味着它正在运行实际的方法。
I have stubbed a method and asked it to return a specific value. However when running the unit tests the real method gets called and returns a real value generated value.
i.e I stubbed the method get_requisition_number to return the value 1
, but when executing the unit tests it returns the value 2011031100001
Unit Test Code:
it "should have a unique requisition number when saved" do
Requisition.stub(:get_requisition_number).and_return("1")
req1 = Requisition.new
req1.save
req2 = Requisition.new
lambda { req2.save! }.should raise_error(ActiveRecord::ActiveRecordError)
end
The method get_requisition_number is called when the save method is executed. I assume from the stub it should return 1
. However it returns a date specific value like 2011031100001
, which means it's running the actual method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在对象实例上存根方法,而不是在类上。
当您对类本身进行存根处理时,您会在 Requisition 类上创建一个
get_requisition_number
方法,例如Requisition.get_requisition_number
。You need to stub the method on the object instances, not the class.
When you stub the class itself, you create a
get_requisition_number
method on the Requisition class, e.g.Requisition.get_requisition_number
.