编辑阅读我对此问题的评论
我对铁轨很新,所以请耐心等待。 我一直在尝试使用工厂女孩和rspec为Devise配置测试。这花了我2个小时的最好的部分,并搜索一半的互联网无济于事。即使有很多线程似乎是我的问题,我也无法弄明白。
这就是my / spec文件的样子。
GET Home Gives the correct status code
Failure/Error: sign_in user
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2:0x00000106f32558>
# ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>
这是我收到的错误消息,尝试进行以下测试:
user_spec.rb:
require 'spec_helper'
describe "GET Home" do
before do
##I have tried all sorts of things here. I have also tried to define a module in devise.rb (see note below*), and then call that module here instead of the 2 lines below. But I get the same error, no local variable or undefined method for ...
user = FactoryGirl.create(:user)
sign_in user
end
describe "GET /Home"
it "Gives the correct status code" do
get root_path
response.status.should be(200)
end
end
in spec/factories/users.rb:
FactoryGirl.define do
factory :user do
name "Christoffer"
email "test@test2.com"
password "testtest"
password_confirmation "testtest"
end
end
并且后续行包含在spec_helpers.rb
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, :type => :controller
现在,通过这样做,我得到上面的错误。任何人都可以解释我在这里做错了什么吗?这可能是非常明显的事情,因为我在Rails的方式上并没有那么好的排练。
*注意(模块我试图在devise.rb中定义并插入before do
):
module ValidUserRequestHelper
# Define a method which signs in as a valid user.
def sign_in_as_a_valid_user_nommels
# ASk factory girl to generate a valid user for us.
@user ||= FactoryGirl.create :user
# We action the login request using the parameters before we begin.
# The login requests will match these to the user we just created in the factory, and authenticate us.
post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
end
end
答案 0 :(得分:0)
'spec / requests'的目的是进行集成测试。您可以从用户的角度测试应用程序的功能(即填写某些信息,然后单击按钮,如果某些输入有效或无效,则会发生这种情况)。规格/型号和规格/控制器通常用于测试应用程序较小部分的单元测试(即,如果传递给用户模型的密码和password_confirmation参数不匹配,会发生什么情况)