每次运行此命令时,我都会一直得到wrong number of arguments (1 for 0)。我究竟做错了什么?

describe Payment do
  before  { @payment = build_stubbed(:payment) }
  subject { @payment }

  shared_examples 'a foreign key' do |key|
    it "can't be nil, blank, or not an int" do
      [nil, "", " ", "a", 1.1].each do |value|
        @payment.send key, value
        @payment.should_not be_valid
      end
    end
  end

  describe "validation" do
    describe "order_id" do
      it_behaves_like 'a foreign key', :order_id
    end
  end
end


失败信息:

1) Payment validation order_id behaves like a foreign key can't be nil, blank, or not an int
     Failure/Error: @payment.send(key, value)
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "a foreign key" called from ./spec/models/payment_spec.rb:27
     # ./spec/models/payment_spec.rb:18:in `block (4 levels) in <top (required)>'
     # ./spec/models/payment_spec.rb:17:in `each'
     # ./spec/models/payment_spec.rb:17:in `block (3 levels) in <top (required)>'

最佳答案

我没有正确提供设置方法:@payment.send(key, value)

相反,我需要使用@payment.send("#{key}=", value)

关于ruby-on-rails - 如何在RSpec中编写可重用的共享示例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16430461/

10-12 14:24