gpt4 book ai didi

ruby - Rspec:辅助方法未被识别

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:13 25 4
gpt4 key购买 nike

在下面的代码中,我在标有注释的行(靠近代码段底部)收到错误:undefined methodmock_transaction_input'`

我不明白为什么,因为上面定义了方法mock_transaction_input。我找不到我正在做的事情与 this article. 中的第一个示例之间的区别.

非常感谢任何帮助!

require 'spec_helper'

describe Transaction do

def mock_transaction_input input
ret = double "TransactionInput"
input.each do |method, mock_return|
ret.stub(method).and_return(mock_return)
end
ret
end

describe "basic calculations" do

examples = {
:example_one => {
:in => {:amount => 10, :alcohol => 5, :tip => 2, :anywhere_credit => 3, :merchant_credit => 1, :initial_subsidy => 0.25, :initial_subsidy_per => 1, :repeat_subsidy => 0, :repeat_subsidy_per => 0, :deal_subsidy => 0, :our_cut => 0.2, :tax_rate => 0.0825, :cc_fee_waived? => false, :cut_on_tax? => true, :cut_on_tip? => true, :deal_redeemed? => false},
:out => { :our_cut => -0.11 , :merchant_cut => 12.58, :gross_sale => 17, :net_sale => 16.25, :merchant_cc_fee => 0.42}
},

}

examples.each do |name, input_output|

input = input_output[:in]
output = input_output[:out]

# ERROR HERE
transaction = Transaction.new(mock_transaction_input input)

output.each do |method, expected_return|
it "should return #{expected_return} when #{method} is called" do
transaction.send(method).should == expected_return
end
end
end
end
end

最佳答案

快速回答 - 您只能在 itspecify 上下文中使用 mock_transaction_input 方法。为了在示例组中使用此方法,您必须将此方法定义为类方法 def self.mock_transaction_input

查看文档:https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module

关于ruby - Rspec:辅助方法未被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246300/

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