[英]How to test via rspec that session variables updated in controller
I am trying to write a test using rspec that tests if the the session variable was correctly changed: 我正在尝试使用rspec编写测试,测试会话变量是否已正确更改:
This is the part of the GamesController I want to test: 这是我要测试的GamesController的一部分:
def change_player
if session[:player] == 0
session[:player] = 1
else
session[:player] = 0
end
end
This is my game_spec.rb file: 这是我的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
This is the error message I get when I run the test: 这是我运行测试时收到的错误消息:
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
Thanks 谢谢
the problem is that rspec doesn't know this is a controller test (is not in spec/controllers
folder) 问题是rspec不知道这是一个控制器测试(不在spec/controllers
文件夹中)
you need to specify that 你需要指定它
describe GamesController, :type => :controller do
...
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.