gpt4 book ai didi

ruby-on-rails - 如何在 RSpec 中编写可重用的共享示例

转载 作者:行者123 更新时间:2023-11-28 21:32:08 25 4
gpt4 key购买 nike

每次运行此程序时,我总是得到一个错误数量的参数(1 代表 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)>'

最佳答案

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com