rail3 / rspec / devise:rspec控制器测试失败,除非我添加一个dummy = subject.current_user.inspect [英] rail3/rspec/devise: rspec controller test fails unless I add a dummy=subject.current_user.inspect
问题描述
我试图让一个简单的脚手架应用程序的RSpec工作,从rspec脚手架测试开始。
I'm trying to get RSpec working for a simple scaffolded app, starting with the rspec scaffold tests.
根据设计维基,我添加了各种设计配置条目,用户和管理员的工厂,我在规范控制器中执行的第一件事是login_admin。
Per the devise wiki, I have added the various devise config entries, a factory for a user and an admin, and the first things I do in my spec controller is login_admin.
最奇怪的是,尽管我的所有规范失败,除非我在之后添加以下声明:
行:
Weirdest thing, though... all my specs fail UNLESS I add the following statement right after the it ... do
line:
dummy=subject.current_user.inspect
(如下图所示,规则通过,没有这一行,所有测试失败,赋值为零,而不是预期值,我只是发现当我放置一些put语句来查看current_user是否被正确设置。)
(With the line, as shown below, the specs pass. Without that line, all tests fail with the assigns being nil instead of the expected value. I only happened to discover that when I was putting some puts statements to see if the current_user was being set correctly.)
所以它的行为就是虚拟语句,以某种方式强制current_user被加载或刷新或被识别。
So what it acts like is that dummy statement somehow 'forces' the current_user to be loaded or refreshed or recognized.
解释发生了什么,我应该做的不同,所以我不需要虚拟语句?
Can anyone explain what's going on, and what I should be doing differently so I don't need the dummy statement?
#specs/controllers/brokers_controller_spec.rb
describe BrokersController do
login_admin
def valid_attributes
{:name => "Bill", :email => "rspec_broker@example.com", :company => "Example Inc", :community_id => 1}
end
def valid_session
{}
end
describe "GET index" do
it "assigns all brokers as @brokers" do
dummy=subject.current_user.inspect # ALL SPECS FAIL WITHOUT THIS LINE!
broker = Broker.create! valid_attributes
get :index, {}, valid_session
assigns(:brokers).should eq([broker])
end
end
describe "GET show" do
it "assigns the requested broker as @broker" do
dummy=subject.current_user.inspect # ALL SPECS FAIL WITHOUT THIS LINE!
broker = Broker.create! valid_attributes
get :show, {:id => broker.to_param}, valid_session
assigns(:broker).should eq(broker)
end
end
并且每个devise wiki这里是我如何登录:用户或:admin
and per the devise wiki here is how I login a :user or :admin
#spec/support/controller_macros.rb
module ControllerMacros
def login_admin
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in Factory.create(:admin) # Using factory girl as an example
end
end
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = Factory.create(:user)
user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
sign_in user
end
end
end
推荐答案
什么斗争!谢谢罗宾,我已经在谷歌搜索了几个小时,最后看到你的帖子;现在我的控制器测试正在工作:)
What a struggle! Thank you Robin, I've been googling on this for hours and finally saw your post; now my controller tests are working :)
要添加到您的答案,我想出了如何将设计会话转换为valid_session哈希,这允许控制器测试运行正确地由rails生成。
To add to your answer, I figured out how to get the devise session into the valid_session hash, which allows the controller tests to run properly as generated by rails.
def valid_session
{"warden.user.user.key" => session["warden.user.user.key"]}
end
这篇关于rail3 / rspec / devise:rspec控制器测试失败,除非我添加一个dummy = subject.current_user.inspect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!