这是我编写的规范的代码。我试图为一个数组(付款)做一个,但是当我尝试在它上面添加let变量时,它告诉我变量或方法不存在。所以我假设变量没有被调用。
任何人都可以看看我应该如何重新规范我的规范,以便我可以做这样的事情:
payments.each do |p|
it "updates the payment record of #{p.id} with a status of #{p.status}" do
payments.each do |p|
payment = Payment.find_by_id(p.id)
payment_status = payment.status
payment_status = p.status
subject
expect(Payment.find_by_id(p.id).status).to_not eq(payment_status)
end
end
end
原始规格代码:
describe "PATCH #submit" do
subject { patch :pay, :payment => [payments] }
context "when the submission is valid" do
let(:payments) { [] }
let(:admin) { FactoryGirl.create(:admin) }
let(:member) { FactoryGirl.create(:member) }
let(:user) { FactoryGirl.create(:user }
before :each do
rand(2..4).times { payments << FactoryGirl.create(:payment, :payor => member, :merchant => user) }
sign_in admin
end
it "updates the status of the payment" do
p = payments.sample #get a random payment submitted
payment = Payment.find_by_id(p.id)
payment_status = payment.status
subject
p.reload
expect(p.status).to_not eq(payment_status)
end
it "updates the record in the array" do
payments.each do |p|
payment = Payment.find_by_id(p.id)
payment_status = payment.status
payment_status = p.status
subject
expect(Payment.find_by_id(p.id).status).to_not eq(payment_status)
end
end
end
end
堆栈追踪:
kareem@kareem-VAIO:~/git/dns$ bundle exec rspec ./spec/controllers/payments_controller_spec.rb:320
Coverage report generated for RSpec to /home/kareem/git/dns/coverage. 256 / 673 LOC (38.04%) covered.
/home/kareem/git/dns/spec/controllers/payments_controller_spec.rb:320:in `block (3 levels) in <top (required)>': undefined local variable or method `payments' for #<Class:0x007f91b6085b70> (NameError)
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `subclass'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:219:in `block in define_example_group_method'
from /home/kareem/git/dns/spec/controllers/payments_controller_spec.rb:300:in `block (2 levels) in <top (required)>'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `subclass'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:219:in `block in define_example_group_method'
from /home/kareem/git/dns/spec/controllers/payments_controller_spec.rb:298:in `block in <top (required)>'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `subclass'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:219:in `block in define_example_group_method'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/dsl.rb:41:in `block in expose_example_group_alias'
from /home/kareem/git/dns/spec/controllers/payments_controller_spec.rb:3:in `<top (required)>'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
from /home/kareem/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
from /home/kareem/.rbenv/versions/2.1.3/bin/rspec:23:in `load'
from /home/kareem/.rbenv/versions/2.1.3/bin/rspec:23:in `<main>'
答案 0 :(得分:1)
您不能在规范之外使用let
定义的变量。如果你真的想让它可用,请在你的规范上定义一个方法并像这样使用它。