Ruby on rails 如何使用rspec初始化中的存根方法
需要“spec\u helper”Ruby on rails 如何使用rspec初始化中的存根方法,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,需要“spec\u helper” class Party attr_accessor :number include Validations def initialize self.number = proccess_args(2) puts luhn? end def proccess_args(opt) ARGV[opt].downcase end end spec_helper.rb 需要_
class Party
attr_accessor :number
include Validations
def initialize
self.number = proccess_args(2)
puts luhn?
end
def proccess_args(opt)
ARGV[opt].downcase
end
end
spec_helper.rb
需要_相对'../'
describe Party do
before :each do
new_method = Party.method(:new)
allow(self).to receive(proccess_args).with(2).and_return('add')
@party = Party.new
end
end
我如何使用rspec来确保process_args没有被实际调用,而是被模拟以避免ARGV破坏我的测试?例如,我想假装调用proccess_args(2)返回“Jason Wade”,并避免ARGV[opt]被rspec触摸 允许(ARGV).接收(:downcase).和返回('whatever')
您告诉RSpec允许
ARGV
接收一个名为的方法:downcase并返回
,而不是在测试的代码中实际调用它。(这部分可能不是特别有用,但正是这种解释让我想起了它。)你能试试吗,它似乎不起作用,而且在处理过程中失败了:(编辑原始帖子,为您的测试文件添加代码。我发布的这行代码只是存根本身,不是测试。完成后,我只是尝试构建该对象来编写测试。我使用ruby 2.1.3p242和rspec(3.2.0),但仍然失败,我不确定为什么。显示规范文件将有助于在此添加测试
require 'yaml'