Rails 4 rspec 3 controller test: session helper module not working for before(:all), works for before(:each) -


i'm building toy chat application using rails 4.2.7, , writing specs controllers using rspec 3.5. api::chatroomscontroller requires user logged in in order create chatroom, have created api::sessionshelper module create sessions within api::chatroomscontroller spec.

# app/helpers/api/sessions_helper.rb module api::sessionshelper   def current_user     user.find_by_session_token(session[:session_token])   end    def create_session(user)     session[:session_token] = user.reset_session_token!   end    def destroy_session(user)     current_user.try(:reset_session_token!)     session[:session_token] = nil   end end   # spec/controllers/api/chatrooms_controller_spec.rb require 'rails_helper' include api::sessionshelper  rspec.describe api::chatroomscontroller, type: :controller   before(:all)     databasecleaner.clean     user.create!({username: "test_user", password: "asdfasdf"})   end    user = user.find_by_username("test_user")    context "with valid params"     done = false      # doesn't work if using before(:all) hook     before(:each)       until done         create_session(user)         post :create, chatroom: { name: "chatroom 1" }         done = true       end     end      let(:chatroom) { chatroom.find_by({name: "chatroom 1"}) }     let(:chatroom_member) { chatroommember.find_by({user_id: user.id, chatroom_id: chatroom.id}) }      "responds successful status code"       expect(response).to have_http_status(200)     end      "creates chatroom in database"       expect(chatroom).not_to eq(nil)     end      "adds chatroom creator chatroommember table"       expect(chatroom_member).not_to eq(nil)     end   end  end 

i'm using before(:each) hook boolean variable done achieve behavior of before(:all) hook creating single session.

if use before(:all) hook, error:

nomethoderror: undefined method `session' nil:nilclass` 

i put debugger in create_session method of api::sessionshelper module check self.class , in both cases, when use before(:each) , when use before(:all), class is:

rspec::examplegroups::apichatroomscontroller::withvalidparams 

however when using before(:each) hook, session {}, while in before(:all) hook, session gives nomethoderror above.

anybody know causes error?

you need include helper in test block:

rspec.describe api::chatroomscontroller, type: :controller   include api::sessionshelper end 

you can avoid duplication including common spec helpers in spec/rails_helper.rb

rspec.configure |config|   # ...   config.include api::sessionshelper, type: :controller end 

this should put database_cleaner config. should use clean between every spec not before lead test ordering issues , flapping tests.

require 'capybara/rspec'  #...  rspec.configure |config|    config.include api::sessionshelper, type: :controller   config.use_transactional_fixtures = false    config.before(:suite)     if config.use_transactional_fixtures?       raise(<<-msg)         delete line `config.use_transactional_fixtures = true` rails_helper.rb         (or set false) prevent uncommitted transactions being used in         javascript-dependent specs.          during testing, app-under-test browser driver connects         uses different database connection database connection used         spec. app's database connection not able access         uncommitted transaction data setup on spec's database connection.       msg     end     databasecleaner.clean_with(:truncation)   end    config.before(:each)     databasecleaner.strategy = :transaction   end    config.before(:each, type: :feature)     # :rack_test driver's rack app under test shares database connection     # specs, continue use transaction strategy speed.     driver_shares_db_connection_with_specs = capybara.current_driver == :rack_test      if !driver_shares_db_connection_with_specs       # driver external browser app       # under test *not* share database connection       # specs, use truncation strategy.       databasecleaner.strategy = :truncation     end   end    config.before(:each)     databasecleaner.start   end    config.append_after(:each)     databasecleaner.clean   end  end 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -