Controller specification not registered in my user created factory

I have a problem with my controller checks for my Course Controller. It seems that the application will not sign my user correctly. All generated controller tests for this controller only fail.

I create my user in users.rb using Factory -girl as below ...

FactoryGirl.define do factory :user do sequence :email do |n| "test#{n}@email.com" end password "password" password_confirmation "password" end end 

Then in my courses_controller_spec.rb I mimic the login as described below.

 require 'spec_helper' describe CoursesController do include Devise::TestHelpers before(:each) do #@request.env["devise.mapping"] = Devise.mappings[:user] user = Factory.create(:user) user.toggle!(:admin) sign_in user end describe "DELETE destroy" do it "redirects to the courses list" do course = Factory.create(:course) delete :destroy, {:id => course.to_param}, valid_session response.should redirect_to(courses_url) end end 

And I get the conclusion ...

 Failure/Error: response.should redirect_to(courses_url) Expected response to be a redirect to <http://test.host/courses> but was a redirect to <http://test.host/users/sign_in> 

Note that I also used the following in my spec_helper.rb

 config.include Devise::TestHelpers, :type => :controller 

And I tried this as https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29

In my query specifications, I can create a user and login using the following, which works fine, but I would also like to get all the controller tests.

 fill_in "Email", :with => user.email fill_in "Password", :with => user.password click_button "Sign in" 

Any help here would be greatly appreciated.

+4
source share
1 answer

I just figured it out myself. Remove valid_session from your delete call. This seems to overwrite the session defined using the test assistants.

Everything else seems right. Save all installation code and just change the delete line in your specification so that:

 delete :destroy, {:id => course.to_param} 

I also contained the following line in the before( :each ) block that you commented out. Not sure what he is doing:

 @request.env["devise.mapping"] = Devise.mappings[:user] 
+6
source

Source: https://habr.com/ru/post/1394723/


All Articles