rspec capybara - 子域测试

时间:2012-02-24 07:13:47

标签: ruby-on-rails ruby-on-rails-3 rspec capybara

当我使用capybara / rspec创建集成测试时,似乎可以获得正确的url和路径。但它在'click_button'上出错了。

注意:'puts current_url'会返回正确的路径:http://StoreName1.lvh.me/login

测试

require 'spec_helper'

describe "Account access" do

  before(:each) do
    Factory(:plan)
    Factory(:theme)
    @account = Factory(:account)
    set_host("#{@account.url}")
    end

  describe "GET /login" do
    it "should not sign in for invalid details" do
      Factory(:account, :url => 'dan', :email => 'dan@example.com')
      visit "/login"
      page.should have_content("Sign in to your store")
      fill_in "email", with: "bob@example.com"
      fill_in "password", with: "donuts"
      click_button 'Sign in'
    end

  end

    def set_host(host)
    host! host
    Capybara.app_host = "http://" + host + ".lvh.me"
    end

end

错误日志:

  1) Account access GET /login should not sign in for invalid details
     Failure/Error: click_button 'Sign in'
     ActiveRecord::RecordNotFound:
       Couldn't find Account with url = StoreName1
     # ./app/controllers/application_controller.rb:11:in `get_account_info'
     # (eval):2:in `click_button'
     # ./spec/requests/accounts_spec.rb:19:in `block (3 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:2)

我认为您的设置主机应该更像:

def set_host(host)
  host! = "http://#{host}.lvh.me:#{Capybara.server_port}"
end

def host!(&block)
  before do
    Capybara.current_session.driver.reset!
    host! instance_eval(&block)
    @old_app_host, Capybara.app_host = Capybara.app_host, "http://#{host}"
    @old_default_host, Capybara.default_host = Capybara.default_host, "http://#{host}"
  end

  after do
    Capybara.current_session.driver.reset!
    Capybara.app_host = @old_app_host
    Capybara.default_host = @old_default_host
  end
end