【问题标题】:Rspec test fails with decent_exposure gemRspec 测试因体面_exposure gem 而失败
【发布时间】:2012-07-17 12:35:59
【问题描述】:

我在我的 Rails 应用程序中使用 RSpec 和 decent_exposure gem 时遇到了问题。

我的控制器测试失败了,因为正版曝光两次调用方法“new”(Model.new(params[name])。一次使用名称 (Brand.new(params["brands"]) 返回 Brand.new(nil )) 和我期望的第二个 (Brand.new(params["brand"]))。我需要以某种方式跳过我的测试文件中的第一个调用。Brand.should_receive(:new).with(...).once.and_return (乐队)不工作。

我的测试文件:

let(:brand) {
  mock_model(Brand).as_null_object
}

before do
  Brand.stub(:new).and_return(brand)
end

describe "with valid parameters" do
  it "should create a new brand" do
    Brand.should_receive(:new).with(
      "name" => "LG",
    ).and_return(brand)

    post :create, :brand => {
      "name" => "LG",
    }
  end
end

那么,你能帮我弄清楚如何通过这个吗?

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    试试这个:

    Brand.should_receive(:new).once.with(any_args())
    Brand.should_receive(:new).once.with("name" => "LG").and_return(brand)
    

    我建议对控制器用来持久化brand 的任何方法添加一个期望值。通常是save:

    brand.should_receive(:save) { true }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多