考虑使用Rails辅助方法log_out
:
def log_out
session.delete(:user_id)
@current_user = nil
end
和rspec帮助规范测试:
describe "#log_out" do
before do
helper.instance_variable_set(:@current_user, "FAKE")
session[:user_id] = "FAKE"
end
it "sets the current user to nil" do
helper.log_out
expect(helper.instance_variable_get(:@current_user)).to be_nil
end
end
此测试通过。但是,当我用instance_variable_set
替换before
块中的assign
时,就像这样:
describe "#log_out" do
before do
assign(:current_user, "FAKE")
session[:user_id] = "FAKE"
end
#...
测试失败了:
Failure/Error: expect(helper.instance_variable_get(:@current_user)).to be_nil
expected: nil
got: "FAKE"
测试正在尝试验证是否已为实例变量分配nil
。我的意图是使用assigns
代替instance_variable_set
。难道我不能使用assign
并测试将该实例变量分配给 new 值的方法吗?