Rspec 分配局部变量?

发布于 2024-12-13 14:54:46 字数 715 浏览 3 评论 0原文

只是想知道是否有一种方法可以访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-12-20 14:54:46

基本上没有。将其视为黑盒测试。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文