Question

I have a very simple rails controller test that behaves differently depending on how it is run:

When I run it using: RAILS_ENV=test ruby -Itest test/controllers/news_controller_test.rb the test passes fine (I used RAILS_ENV to try and make it as similar to rake test as I can.

When run using rake test, the test fails with:

12) Error: NewsController::admin#test_0003_creates a new news post: ActionView::MissingTemplate: Missing template news/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :jbuilder, :coffee]}. Searched in: * "/home/emery/eop/portal/app/views" * "/home/emery/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/kaminari-0.15.1/app/views"

test/controllers/news_controller_test.rb:73:in `block (4 levels) in <top (required)>'
test/controllers/news_controller_test.rb:72:in `block (3 levels) in <top (required)>'

This is only one specific case where a test fails under rake test but passes with ruby -Itest ... There are many others that behave this way. Some, like this one, are missing templates which they shouldn't need, others are returning success when they should be redirecting the user, and others are more esoteric. Which is why my question is more general, what are the key differences between rake test and RAILS_ENV ruby -Itest ...? I'd like to be able to debug these myself.

My stack is Rails 4.1rc1, using minitest-spec-rails (master branch).

For completeness, here is the test and controller code for the error above:

The controller action is pretty simple:

  def create
    @news = News.new(safe_params)
    if @news.save
      redirect_to news_index_path
    else
      render :new
    end 
  end 

The code either redirects or renders the new template, so it's true that news/create doesn't exist, but it shouldn't be needed.

The controller test code is:

let(:editor) { User.create! first_name: 'Malcom', last_name: 'Reynolds', email: 'captain@firefly.com', password: 'hmm' }

  before do
    subject.login editor
  end

...

  it 'creates a new news post' do
     assert_difference 'News.count', 1 do                                                                                                                                                                                                                                                                                                                                  
       post :create, news: { subject: 'abcd', body: 'rst' }
     end
     assert_redirected_to news_index_path
  end

(subject.login is a custom login function on the application controller)

Était-ce utile?

La solution

It turned out to be an issue with one of my other controller test cases. I had one controller test that mocked a few calls out, but those mocks were inadvertently permanent, changing the application controller. This broke the remaining controller test cases in different ways.

So the key difference in this case was that running rake test runs all the test cases, and ruby -Itest only runs one test.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top