Rspec 分配局部变量?
只是想知道是否有一种方法可以访问 rspec 中的局部变量而不将它们转换为实例变量?为了解释我的问题:
我有以下操作:
def queue_due_mail
payments = Payment.due_soon.where(:send_reminder => true)
payments.each do |p|
PaymentMailer.delay.reminder_email(p)
p.send_reminder = false
p.save
end
redirect_to root_path
end
而且,在我的规范中,我想运行这样的操作:
it "should assign nearly due payments to payments" do
Payment.stub_chain(:due_soon, :where) { [mock_payment] }
get :queue_due_mail
assigns[:payments].should eq([mock_payment])
end
问题是,只有当我将局部变量“付款”转换为“@”时,分配[:付款]调用才有效付款”。这不是一个主要问题,但我不想让我的 rspec 测试受到实际代码的影响。
那么,有没有办法在 rspec 赋值中引用局部变量呢?
干杯...
Just wondering if there's a way to access local variables in rspec without turning them into instance variables? To explain my issue:
I have the following action:
def queue_due_mail
payments = Payment.due_soon.where(:send_reminder => true)
payments.each do |p|
PaymentMailer.delay.reminder_email(p)
p.send_reminder = false
p.save
end
redirect_to root_path
end
And, in my spec, I want to run something like this:
it "should assign nearly due payments to payments" do
Payment.stub_chain(:due_soon, :where) { [mock_payment] }
get :queue_due_mail
assigns[:payments].should eq([mock_payment])
end
The problem is that the assigns[:payments] call only works if I turn the local variable 'payments' into '@payments'. This isn't a major problem, but I'd rather not have my rspec tests influence by actual code.
So, is there a way to reference local variables in rspec assigns?
Cheers...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上没有。将其视为黑盒测试。 rspec 测试进入或离开方法的内容(控制器操作是一种方法)。任何局部变量都应该在方法结束时被丢弃 - 因此在方法调用结束时无法进行测试。
basically no. Think of it as black-box testing. rspec tests what goes into or out of a method (a controller action is a method). Any local variables should be thrown away at the end of the method - therefore can't be tested when the method-call is over.