我在我的tests.There中使用了Rspec,FactoryGirl和Spork,这是两件我不太清楚的事情,首先是我的factories.rb文件的位置。目前我已经把它放在
spec/support/factories.rb它看起来像这样
FactoryGirl.define do
factory :user do
email "example@yahoo.com"
password "password"
password_confirmation "password"
confirmed_at Time.now
end
end在我的spec_helper中我有
config.include FactoryGirl::Syntax::Methods其次,在开始对控制器进行测试之前,我想登录一个用户,这个特定的控制器有一个for filter :authenticate_user!
我正在使用devise进行身份验证,所以我添加了
config.include Devise::TestHelpers, :type => :controller阅读devise文档,您可以添加controller_macros.rb并指定要使用的方法
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.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所以我把这个也添加到了我的spec_helper中
config.include ControllerMacros, :type => :controller因此,当我在控制器测试之前添加login_user时,我得到了未定义的方法login_user。我是否使用了两个工具来做同样的事情?我是否真的需要设计方法,或者这一切都可以用factoryGirl来完成。如果是这样,我如何设置登录过程,然后才能测试控制器?
发布于 2013-01-31 20:09:44
工厂位置应在spec/factories中。查看此示例应用程序https://github.com/RailsApps/rails3-devise-rspec-cucumber/tree/master/spec。
对于登录,一般来说,你似乎做得很对。再次检查示例应用程序,并单击此处:https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29
对于undefined method login_user错误,请确保
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}和
config.extend ControllerMacros, :type => :controller在spec_helper中。设计方法应适用于以下主题:
subject.current_user.should_not be_nilhttps://stackoverflow.com/questions/13394203
复制相似问题