1

I have a very simple controller:

class ThingsController < ApplicationController
  def create
    Thing.do_stuff
  end
end

…And a very simple spec:

require "rails_helper"

describe ThingsController do
  describe "POST #create" do
    it "does stuff with things" do
      expect(Thing).to receive(:do_stuff)
      controller.create # This works
      post :create # This does not work
    end
  end
end

I am not running the direct invocation and the post request at the same time. Invoking the action on the controller directly passes the assertion, but invoking the action through the post method does not. It appears do_stuff is never called on Thing. Why might that be?

1
  • Do you get anything useful from response.body? Commented May 2, 2015 at 15:33

1 Answer 1

2

I discovered what my issue was.

Invoking the controller directly keeps the spec isolated and ignores things like a before_action in the ApplicationController.

When we start using the post method, it’s really an integrated test and hits things like authentication. I couldn’t hit my controller method because my test user wasn’t signed in.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.