ruby-on-rails - 为什么我的测试因参数错误(预期的 URI 对象或 URI 字符串)而失败?

标签 ruby-on-rails ruby-on-rails-4 rspec factory-bot

我有一个场景,我必须根据 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

更新:

我刚刚注意到对于 sign_up 场景 request.referer 返回 nil 但对于其他sign_in 场景 它返回 "http://www.example.com/" (问题中省略了)。

我做错了什么?

最佳答案

您可能遇到了 capybara 的 headless 浏览器没有引荐来源网址的问题,因为您直接将浏览器“打开”到 new_user_registration_path 导致了 nil 引荐来源网址。想象一下,在 word 文档中单击该 url,然后您的计算机打开带有该 url 的 chrome...将没有引荐来源网址。

因此请尝试使用以下内容替换:visit new_user_registration_path:

visit root_path
click_link('Sign Up')

然后也许它会有一个推荐人,你的测试就会通过。

关于ruby-on-rails - 为什么我的测试因参数错误(预期的 URI 对象或 URI 字符串)而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32049553/

相关文章:

ruby-on-rails - 分配(:user) returning nil rather than user

c - C 的测试框架

ruby-on-rails - 编写此 if 子句的更好/更清晰的方式

mysql - Rails - 将表存档到另一个数据库

ruby-on-rails - 在 SASS 中访问 Ruby 变量(从模型或 Controller )

ruby-on-rails - 在ActiveModel::Serializer中序列化错误哈希

ruby - link_to 中的 XSS 不安全参数值

ruby-on-rails - block 范围不一致之前的 Rspec

ruby-on-rails - Rspec Controller 错误,要求<"index">,但使用<""进行渲染>

javascript - 单击选择框 - Rails