RSpec - 如何测试包括HTTParty的类

时间:2015-05-01 08:39:29

标签: ruby rspec httparty

我有这样的代码:

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

但它无法正常工作。如何解决?

1 个答案:

答案 0 :(得分:-1)

将您的OR更改为以下内容:

subject