Подтвердить что ты не робот

Как проверить через rspec, что переменные сеанса, обновленные в контроллере

Я пытаюсь написать тест с помощью rspec, который проверяет правильность изменения переменной сеанса:

Это часть GameController, которую я хочу проверить:

def change_player
  if session[:player] == 0
    session[:player] = 1
  else
    session[:player] = 0
  end    
end

Это мой файл game_spec.rb:

require "spec_helper"

describe GamesController do
 describe "#change_player" do
   it "should change player to 1" do
     session[:player] = 0

     get :change_player

     assigns(session[:player]).should == 1
   end
 end
end

Это сообщение об ошибке, которое я получаю при запуске теста:

Failures:

  1) GamesController#change_player should set player to 0
     Failure/Error: session[:player] = 0
     NameError:
       undefined local variable or method `session' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000103b3b0d8>
     # ./spec/features/game_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.01709 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/game_spec.rb:5 # GamesController#change_player should set player to 0

Спасибо

4b9b3361

Ответ 1

проблема в том, что rspec не знает, что это тест контроллера (не находится в папке spec/controllers)

вам нужно указать, что

describe GamesController, :type => :controller do
    ...
end