RocketGuy3
RocketGuy3

Reputation: 625

Rspec controller test does not hit my controller action

I added an import method to a controller, and it works just fine when I test it manually from my website, but it is failing in rspec. Here is what my test looks like:

require 'spec_helper'

describe PropertiesController do
  let!(:user) { FactoryGirl.create(:user) }

  before :each do
    sign_in user
  end

  describe "should upload user properties" do
    before do
      post :import, spreadsheet: fixture_file_upload("/files/property_upload_template.xlsx")
    end

    it "should have created records" do
      expect(Property.count).to eq 3
      # Some other assertions
    end
  end
end

When I add puts statements inside my import action, including on the very first line, none of them are apparently invoked. The test is generating no errors other than failing the assertions. Similarly, when I look at the test.log file, all that happens is the creation of my test user (and a devise confirmation email gets sent out), but it doesn't appear that the import action is ever hit. The test server seems to recognize the route fine, but it's not actually executing the action.

Is there something wrong with my test configuration?

Upvotes: 5

Views: 1557

Answers (1)

RocketGuy3
RocketGuy3

Reputation: 625

I had been banging my head for a good couple hours, but I just figured it out. I needed to confirm the user in my user factory. I guess since I enabled the confirmable module in devise, and the user wasn't confirmed, it was silently not allowing me to authenticate...

... Would sure be nice if rspec/rails/devise generated some sort of error pointing me to the problem here.

For the sake of completeness, I'm adding in the code for confirming a user in the version of FactoryGirl at the time of that writing:

FactoryGirl.define do
  factory :confirmed_user, :parent => :user do
    after(:create) { |user| user.confirm! }
  end
end

Upvotes: 5

Related Questions