I'm new to RSpec, so bear with me!

Here is my code.

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

Class file:

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

Basically, I have this test.json that I want to use to verify that things are getting parsed correctly. This HTTParty.get call expects a GET call to be made though. Is there a way I can mock that with the param_source variable. Please let me know if I need to provide more information here. Thanks for the help!

有帮助吗?

解决方案

Stub:

HTTParty.stub(get: file_content)

or mock:

HTTParty.should_receive(:get).with(foo, bar).and_return(file_content)

or stubbing in the new rspec syntax:

allow(HTTParty).to receive(:get).and_return(file_content)

or mocking in the new syntax:

expect(HTTParty).to receive(:get).with(foo, bar).and_return(file_content)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top