为什么我的测试因参数错误(预期的 URI 对象或 URI 字符串)而失败?
Why are my tests failing with bad argument (expected URI object or URI string) error?
我有一个场景,我必须将角色分配给用户基于request.referrer
的值(即页面的URL从他们注册的地方)。我通过下面的代码实现了它
#application_controller.rb
private
def store_referrer_url
session[:referrer] ||= URI(request.referer).path if ((self.controller_name == 'registrations' || self.controller_name == 'users/omniauth_callbacks'))
end
我将其作为 hidden_field
的值传递,如下所示
<%= f.hidden_field :referrer_url, :value => session[:referrer] %>
一切正常,但我的测试失败并出现以下错误
2) Sign in user can sign up
Failure/Error: sign_up(user.first_name, user.last_name, user.email, user.encrypted_password, user.encrypted_password)
ArgumentError: bad argument (expected URI object or URI string
./app/controllers/application_controller.rb:100:in `store_referrer_url'
下面是我的测试代码
#/spec/features/users/user_sign_up_spec.rb
include Warden::Test::Helpers
Warden.test_mode!
feature 'Sign up', :devise do
scenario 'user can sign up' do
user = FactoryGirl.create(:user)
sign_up(user.first_name, user.last_name, user.email, user.encrypted_password, user.encrypted_password)
end
end
#/spec/features/support/helpers/session_helpers.rb
module Features
module SessionHelpers
def sign_up(firstname, lastname, email, password, confirmation)
visit new_user_registration_path
fill_in 'first_name', with: firstname
fill_in 'last_name', with: lastname
fill_in 'email', with: email
fill_in 'user_password', with: password, :match => :first
fill_in 'user_password_confirmation', :with => confirmation
find("#user_referrer_url").set("/")
click_button 'Register'
end
end
end
#/spec/factories/users.rb
require 'faker'
FactoryGirl.define do
factory :user do |f|
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
f.email { Faker::Internet.email }
f.encrypted_password { Faker::Lorem.characters(10) }
f.password { Faker::Lorem.characters(10) }
f.primary_phone { Faker::PhoneNumber.cell_phone }
f.alternate_phone { Faker::PhoneNumber.phone_number }
f.role [0,1].sample
f.agree_tos true
confirmed_at = Time.now
f.referrer_url ["/", "/visitors/owner-faq"].sample
end
end
更新:
我刚刚注意到 request.referer
returns for nil
for sign_up scenario 但它 returns "http://www.example.com/"
对于其他 sign_in 场景(问题中省略) .
我做错了什么?
您正在将路径作为参数传递以创建 URL
对象。
要使其正常工作,您需要将主机添加到您的引荐来源网址值。
您可能 运行 遇到了水豚的无头浏览器没有引荐来源网址的问题,因为您 'opened' 浏览器直接指向 new_user_registration_path 导致无引荐来源网址。想象一下,在 word 文档中单击 url,然后您的计算机打开 chrome 并显示 url...将没有引荐来源网址。
所以请尝试将 visit new_user_registration_path
替换为以下内容:
visit root_path
click_link('Sign Up')
那么也许它会有一个推荐人,你的测试就会通过。
我有一个场景,我必须将角色分配给用户基于request.referrer
的值(即页面的URL从他们注册的地方)。我通过下面的代码实现了它
#application_controller.rb
private
def store_referrer_url
session[:referrer] ||= URI(request.referer).path if ((self.controller_name == 'registrations' || self.controller_name == 'users/omniauth_callbacks'))
end
我将其作为 hidden_field
的值传递,如下所示
<%= f.hidden_field :referrer_url, :value => session[:referrer] %>
一切正常,但我的测试失败并出现以下错误
2) Sign in user can sign up
Failure/Error: sign_up(user.first_name, user.last_name, user.email, user.encrypted_password, user.encrypted_password)
ArgumentError: bad argument (expected URI object or URI string
./app/controllers/application_controller.rb:100:in `store_referrer_url'
下面是我的测试代码
#/spec/features/users/user_sign_up_spec.rb
include Warden::Test::Helpers
Warden.test_mode!
feature 'Sign up', :devise do
scenario 'user can sign up' do
user = FactoryGirl.create(:user)
sign_up(user.first_name, user.last_name, user.email, user.encrypted_password, user.encrypted_password)
end
end
#/spec/features/support/helpers/session_helpers.rb
module Features
module SessionHelpers
def sign_up(firstname, lastname, email, password, confirmation)
visit new_user_registration_path
fill_in 'first_name', with: firstname
fill_in 'last_name', with: lastname
fill_in 'email', with: email
fill_in 'user_password', with: password, :match => :first
fill_in 'user_password_confirmation', :with => confirmation
find("#user_referrer_url").set("/")
click_button 'Register'
end
end
end
#/spec/factories/users.rb
require 'faker'
FactoryGirl.define do
factory :user do |f|
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
f.email { Faker::Internet.email }
f.encrypted_password { Faker::Lorem.characters(10) }
f.password { Faker::Lorem.characters(10) }
f.primary_phone { Faker::PhoneNumber.cell_phone }
f.alternate_phone { Faker::PhoneNumber.phone_number }
f.role [0,1].sample
f.agree_tos true
confirmed_at = Time.now
f.referrer_url ["/", "/visitors/owner-faq"].sample
end
end
更新:
我刚刚注意到 request.referer
returns for nil
for sign_up scenario 但它 returns "http://www.example.com/"
对于其他 sign_in 场景(问题中省略) .
我做错了什么?
您正在将路径作为参数传递以创建 URL
对象。
要使其正常工作,您需要将主机添加到您的引荐来源网址值。
您可能 运行 遇到了水豚的无头浏览器没有引荐来源网址的问题,因为您 'opened' 浏览器直接指向 new_user_registration_path 导致无引荐来源网址。想象一下,在 word 文档中单击 url,然后您的计算机打开 chrome 并显示 url...将没有引荐来源网址。
所以请尝试将 visit new_user_registration_path
替换为以下内容:
visit root_path
click_link('Sign Up')
那么也许它会有一个推荐人,你的测试就会通过。