我有这样的密码:

class FooBar
  include HTTParty

  def self.token
    fail Project::MissingToken unless Project::Config.key?("project_token")
    Project::Config.fetch("project_token")
  end

  base_uri "https://website.com/api"
  default_params token: token
end

现在,当Project::Config不包含键并引发异常时,我需要测试一个案例我在考虑这样的事情:
context "token is not given" do
  subject { Project::FooBar }

  it "should raise an error" do
    expect(Project::Config).to receive(:key?).with("project_token").and_return(false)
    expect { subject }.to raise_error(Project::MissingToken)
  end
end

但它不能正常工作。怎么解决?

最佳答案

subject更改为以下值:

subject { described_class.token }

关于ruby - RSpec-如何测试包括HTTParty的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29983900/

10-14 17:34