我是RSpec的新手,所以请耐心等待我!
这是我的代码。
测试文件:
before(:all) do
@package = Package.new("testing")
@param_source = "cat #{root}/file/test.json"
end
"it should update the version params appropriately" do
group_params = mock("params")
group_params.expects(:each).multiple_yields([["staging", @param_source]])
@package.update_version(group_params)
# Some assertions here
end
类文件:
class Package
def initialize(db_file)
# Some http options set for httparty
end
def update_version(group_params)
group_params.each do |environment, param_source|
group_json = JSON.parse(HTTParty.get(param_source, @http_cred))
# Bunch more stuff done here
end
基本上,我有这个test.json,我想用它来验证事情是否正确解析。这个HTTParty.get调用期望进行GET调用。有没有办法用param_source
变量来模拟它。如果我需要在这里提供更多信息,请告诉我。谢谢你的帮助!
答案 0 :(得分:8)
存根:
HTTParty.stub(get: file_content)
或模拟:
HTTParty.should_receive(:get).with(foo, bar).and_return(file_content)
或新的rspec语法中的存根:
allow(HTTParty).to receive(:get).and_return(file_content)
或以新语法嘲笑:
expect(HTTParty).to receive(:get).with(foo, bar).and_return(file_content)