Sha*_*tel 2 ruby testing rspec mocking stubbing
我是RSpec的新手,所以请耐心等待!
这是我的代码.
TESTFILE:
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
Run Code Online (Sandbox Code Playgroud)
班级档案:
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
Run Code Online (Sandbox Code Playgroud)
基本上,我有这个test.json,我想用来验证事情是否正确解析.这个HTTParty.get调用期望进行GET调用.有没有办法可以用param_source变量来模拟它.如果我需要在这里提供更多信息,请告诉我.谢谢您的帮助!
存根:
HTTParty.stub(get: file_content)
Run Code Online (Sandbox Code Playgroud)
或者模仿:
HTTParty.should_receive(:get).with(foo, bar).and_return(file_content)
Run Code Online (Sandbox Code Playgroud)
或者在新的rspec语法中存根:
allow(HTTParty).to receive(:get).and_return(file_content)
Run Code Online (Sandbox Code Playgroud)
或者用新语法嘲笑:
expect(HTTParty).to receive(:get).with(foo, bar).and_return(file_content)
Run Code Online (Sandbox Code Playgroud)