我有一个rails应用程序,它的行为取决于它访问的域名(例如www.myapp.com将以不同方式调用user.myapp.com).在生产中使用这一切都很好但我的测试代码总是看到主机名"www.example.com".
是否有一种干净的方法让测试指定它假装访问的主机名?
解
这很有效.主要的本质是我必须在登录表单中设置Capybara.server_port和Capybara.app_host手动登录.Capybara.app_host除非在变量中声明,否则无法使用动态子域进行设置.所有网址都必须硬编码.
require 'spec_helper'
feature 'customer' do
let(:user) {FactoryGirl.create(:user)}
let(:firm) {user.firm}
let(:customers) {"http://#{firm.subdomain}.lvh.me:31234/customers"}
let(:root_url) {"http://#{firm.subdomain}.lvh.me:31234/"}
before(:all) do
Capybara.server_port = 31234
sub = firm.subdomain
Capybara.app_host = root_url
end
def sign_in_on_js
visit root_url
fill_in "Email", :with => user.email
fill_in "Password", :with => "password"
click_button "Sign in"
page.should have_content("Signed in successfully.")
end
scenario "make new", js: true do
sign_in_on_js
visit customers
page.should have_content("Add new customer")
find("#dialog_customer").click
page.should have_content("Create new customer")
end
end
Run Code Online (Sandbox Code Playgroud)
原始问题 我在rails中制作多租户应用程序.会有很多javascript.但是,我无法让测试工作.
当没有运行时,:js = …
我正在尝试为网页编写请求规范.
此页面正在开发中,没有错误.
但是在capybara webkit中运行我在尝试提交表单后收到此错误:
Failure/Error: Unable to find matching line from backtrace
Capybara::Driver::Webkit::WebkitInvalidResponseError:
Unable to load URL: https://127.0.0.1:33416/sign_in
Run Code Online (Sandbox Code Playgroud)
为了找出原因,我开始在页面上删除标记和JavaScript.到具有普通提交按钮的空表单的程度.我仍然得到上述错误!
该测试现在字面上:
it "should be able create a new foo", :js, :focus do
visit new_foo_path
find('#submit').click
end
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除:js选项,此测试确实有效:
it "should be able create a new foo", :focus do
visit new_foo_path
find('#submit').click
end
Run Code Online (Sandbox Code Playgroud)
Javascript测试在此应用程序的其他页面中有效...
这对我没有意义.有没有人有任何建议如何从这里调试?
谢谢你的帮助
rspec ruby-on-rails capybara ruby-on-rails-3 capybara-webkit