1

好的,我不确定在设置 capybara 时缺少什么,但在测试访问链接时似乎无法正常工作。所以我有一个 3.2 的应用程序,我的 specs_helper 设置如下:

require "spork"

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'factory_girl'
  require 'capybara/rspec'
  require 'capybara/rails'

  Capybara.javascript_driver = :selenium

  Capybara.default_wait_time = 5

  include Devise::TestHelpers # see spec/support/devise.rb
include CarrierWave::Test::Matchers
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
include New_spec_helpers

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  RSpec.configure do |config|

    config.mock_with :rspec


    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    config.use_transactional_fixtures = false


   config.before :each do


       Capybara.default_selector = :css
      if Capybara.current_driver == :rack_test
        DatabaseCleaner.strategy = :transaction
      else
        DatabaseCleaner.strategy = :truncation
      end
      DatabaseCleaner.start


    end

    config.after do
      DatabaseCleaner.clean
    end
  end

end

Spork.each_run do
  FactoryGirl.factories.clear
  FactoryGirl.define do
    sequence(:code) { |n| "code#{n}" }
    sequence(:title) { |n| "title#{n}" }
  end

  Dir[Rails.root.join("spec/factories/**/*.rb")].each{|f| load f}
  PortfolioNew::Application.reload_routes!
end

然后我的测试设置如下:

  before(:each) do
    build_projects
    visit(projects_path)
  end

  it "test js" , :js=>true do
    current_path.should == projects_path
  end

然而,我得到的回报是:

Failure/Error: current_path.should == projects_path
       expected: "/projects"
            got: nil (using ==)

我检查了我的路线,项目路径绝对正确且有效。任何关于这个问题的想法将不胜感激

4

2 回答 2

2

好的,我在类似帖子Rails 3 rspec + capybara - current_path is nil的评论中发现了这个问题?

原来 webrat 与旧测试中遗留下来的 Capybara 冲突。

因此,对于任何感兴趣的人,我只是从我的 gemfile 中评论出来,一切都很好!

于 2012-08-13T20:47:15.910 回答
1

尝试将visit呼叫移动到it块中:

before(:each) do
  build_projects
end

it "test js" , :js=>true do
  visit(projects_path)
  current_path.should == projects_path
end
于 2012-08-13T01:20:03.353 回答