我正在关注Rails 4和Multitenancy的教程,但在尝试测试用户是否可以作为所有者登录并重定向到已创建的子域时遇到错误。
这是测试错误:
Failures:
1) User sign in signs in as an account owner successfully
Failure/Error: visit root_url
URI::InvalidURIError:
bad URI(is not URI?): http://#{account.subdomain}.example.com
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rack_test/browser.rb:71:in `reset_host!'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rack_test/browser.rb:21:in `visit'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rack_test/driver.rb:43:in `visit'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:240:in `visit'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/users/sign_in_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.56981 seconds (files took 1.26 seconds to load)
11 examples, 1 failure, 6 pending
这是测试:
require "rails_helper"
feature "User sign in" do
extend SubdomainHelpers
let!(:account) { FactoryGirl.create(:account) }
let(:sign_in_url) {"http://#{account.subdomain}.example.com/sign_in"}
let(:root_url) {"http://#{account.subdomain}.example.com/"}
within_account_subdomain do
scenario "signs in as an account owner successfully" do
visit root_url
expect(page.current_url).to eq(sign_in_url)
fill_in "Email", :with => account.owner.email
fill_in "Password", :with => "password"
click_button "Sign in"
expect(page).to have_content("You are now signed in.")
expect(page.current_url).to eq(root_url)
end
end
end
以下是工厂:
帐户:
FactoryGirl.define do
factory :account, :class => Subscribe::Account do
sequence(:name) { |n| "Test Account ##{n}" }
sequence(:subdomain) { |n| "test#{n}" }
association :owner, :factory => :user
end
end
用户:
FactoryGirl.define do
factory :user, :class => Subscribe::User do
sequence(:email) { |n| "test#{n}@example.com" }
password "password"
password_confirmation "password"
end
end
我对BDD真的不熟悉,如果您需要我发布任何内容,请告诉我。
答案 0 :(得分:2)
所以我解决了这个问题:
问题出在我的SubdomainHelpers文件
中module SubdomainHelpers
def within_account_subdomain
### This Line Is the original line
let(:subdomain_url) { 'http://#{account.subdomain}.example.com' }
### Changed it to this
let(:subdomain_url) { "http://#{account.subdomain}.example.com" }
before { Capybara.default_host = subdomain_url }
after { Capybara.default_host = 'http://www.example.com' }
yield
end
end
由于某些原因,使用单引号将account.subdomain
保留为字符串;一旦我改为双引号,测试就通过了!
感谢。