Ruby on rails rails 4中rspec特性测试重构代码的问题
我试图让我的web应用程序的用户能够使用密码登录。我使用自己的身份验证,而不是使用gem。我读了这篇关于重构Rspec/Capybara测试的文章: 我喜欢我读到的东西,决定尝试一下重构。我为我的功能测试创建了一个会话帮助文件Ruby on rails rails 4中rspec特性测试重构代码的问题,ruby-on-rails,ruby,rspec,capybara,Ruby On Rails,Ruby,Rspec,Capybara,我试图让我的web应用程序的用户能够使用密码登录。我使用自己的身份验证,而不是使用gem。我读了这篇关于重构Rspec/Capybara测试的文章: 我喜欢我读到的东西,决定尝试一下重构。我为我的功能测试创建了一个会话帮助文件 module Features module SessionHelpers def sign_in user = create(:user) visit '/authentications/new' fill_in 'Lo
module Features
module SessionHelpers
def sign_in
user = create(:user)
visit '/authentications/new'
fill_in 'Login', with: user.name
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
end
然后,我在登录测试中调用了sign_-in函数。这是一个小样本
require 'spec_helper'
feature "signing in" do
before :each do
User.create(:name => 'user@example.com', :password => 'caplin')
end
scenario "user who logs in with correct credentials" do
sign_in
expect(page).to have_content 'Hi user@example.com'
end
end
不幸的是,我一直收到以下错误消息:
2) signing in user who logs in with correct credentials
Failure/Error: sign_in
NoMethodError:
undefined method `create' for #<RSpec::Core::ExampleGroup::Nested_3:0x007ffc85012438>
# ./spec/support/features/session_helpers.rb:4:in `sign_in'
# ./spec/features/user_logs_in_spec.rb:13:in `block (2 levels) in <top (required)>'
2)使用正确凭据登录的登录用户
失败/错误:登录
命名错误:
未定义的方法“create”#
#./spec/support/features/session\u helpers.rb:4:in“登录”
#./spec/features/user_logs_in_spec.rb:13:in'block(2层)in'
基本上,我需要一些方法来抓取我创建的用户并将其传递到sign_-in函数中。有什么提示吗?我猜您的第一个问题是与ThoughBot示例不同的测试配置<代码>创建据我所知不是RSpec中可用的默认方法;我猜他们已经将每个FactoryGirl方法都添加到了测试范围中。如果您使用的是FactoryGirl,则只需对create命令命名即可获得相同的行为:
def sign_in
user = FactoryGirl.create(:user)
visit '/authentications/new'
fill_in 'Login', with: user.name
fill_in 'Password', with: user.password
click_button 'Sign in'
end
然而,这并不能满足您的所有要求,因为您仍然无法添加自定义用户。一种简单的方法是允许传入用户:
def sign_in(user=nil)
user ||= FactoryGirl.create(:user)
...
end
如果您在登录
呼叫中没有传递一个用户,这将为您创建一个用户
回到您发布的规范,您希望将其更改为:
feature "signing in" do
before :each do
@user = User.create(:name => 'user@example.com', :password => 'caplin')
end
scenario "user who logs in with correct credentials" do
sign_in(@user)
expect(page).to have_content 'Hi user@example.com'
end
end
您需要将您创建的用户附加到一个变量(
@user
),然后根据需要将其传递给登录名 我猜您的第一个问题是与ThoughBot示例不同的测试配置<代码>创建
据我所知不是RSpec中可用的默认方法;我猜他们已经将每个FactoryGirl方法都添加到了测试范围中。如果您使用的是FactoryGirl,则只需对create命令命名即可获得相同的行为:
def sign_in
user = FactoryGirl.create(:user)
visit '/authentications/new'
fill_in 'Login', with: user.name
fill_in 'Password', with: user.password
click_button 'Sign in'
end
然而,这并不能满足您的所有要求,因为您仍然无法添加自定义用户。一种简单的方法是允许传入用户:
def sign_in(user=nil)
user ||= FactoryGirl.create(:user)
...
end
如果您在登录
呼叫中没有传递一个用户,这将为您创建一个用户
回到您发布的规范,您希望将其更改为:
feature "signing in" do
before :each do
@user = User.create(:name => 'user@example.com', :password => 'caplin')
end
scenario "user who logs in with correct credentials" do
sign_in(@user)
expect(page).to have_content 'Hi user@example.com'
end
end
您需要将您创建的用户附加到一个变量(@user
),然后根据需要将其传递给登录名 您模型中的问题
module Features
module SessionHelpers
def sign_in
user = create(:user) # <- this method allow only in FactoryGirl
visit '/authentications/new'
fill_in 'Login', with: user.name
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
end
符合规格
feature "signing in" do
let(:login_user) { Features.new }
scenario "user who logs in with correct credentials" do
page = login_user.sign_in
expect(page).to have_content 'Hi user@example.com'
end
end
模型中的问题
module Features
module SessionHelpers
def sign_in
user = create(:user) # <- this method allow only in FactoryGirl
visit '/authentications/new'
fill_in 'Login', with: user.name
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
end
符合规格
feature "signing in" do
let(:login_user) { Features.new }
scenario "user who logs in with correct credentials" do
page = login_user.sign_in
expect(page).to have_content 'Hi user@example.com'
end
end
您可以通过在测试中包含FactoryGirl来实现这一点。您的RSpec配置块(在spec_helper.rb或新版本的RSpec rails_helper.rb中)应该如下所示:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
您可以通过在测试中包含FactoryGirl来实现这一点。您的RSpec配置块(在spec_helper.rb或新版本的RSpec rails_helper.rb中)应该如下所示:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end