Martin
Martin

Reputation: 509

Rspec doesn't fail this trivial code, even though it should

Controller:

class FooController < ApplicationController
  def create
  end
end

Controller spec:

describe FooController
  it "does bar" do
    Foo.should_receive(:new).with("text" => "Lorem ipsum")
    post :create, foo: { "text" => "Lorem ipsum" }
  end
end

When I run this, rspec says that it's a success. However, Foo.new is never called in the create method. If I change the Lorem ipsum in the post function call to something else, however, it fails. I would expect this to fail, and succeed if I added Foo.new(params[:foo]) to the body of the create method. Why is this not the case?

Upvotes: 2

Views: 165

Answers (1)

dmcnally
dmcnally

Reputation: 771

Looks like you are missing a "do" on the describe block. Try:

describe FooController do
  it "does bar" do
    Foo.should_receive(:new).with("text" => "Lorem ipsum")
    post :create, foo: { "text" => "Lorem ipsum" }
  end
end

Upvotes: 2

Related Questions