为什么我的红宝石测试没有找到访问?

时间:2012-12-20 14:25:06

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

我正在使用minitest和rails和capybara,以下测试给了我这个错误...

    undefined method `visit' for #<Page:0x007f8bf3697630>
    STDERR:
    Exception `NoMethodError' at /Users/trace/.rvm/gems/ruby-1.9.3-p194@application.1/gems/activemodel-3.1.8/lib/active_model/attribute_methods.rb:385:in `method_missing'
    /Users/trace/.rvm/gems/ruby-1.9.3-p194@application.1/gems/activerecord-3.1.8/lib/active_record/attribute_methods.rb:60:in `method_missing'
    /Users/trace/.rvm/gems/ruby-1.9.3-p194@application.1/gems/capybara-2.0.1/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
    test/support/utilities.rb:2:in `sign_in_user'
    test/integration/pages_intergration_test.rb:12:in `block (4 levels) in <top (required)>'
    /Users/trace/.rvm/gems/ruby-1.9.3-p194@application.1/gems/mocha-0.9.12/lib/mocha/integration/mini_test/version_201_to_202.rb:27:in `run'

这是我的档案test/integration/pages_intergration_test.rb

require "minitest_helper"

describe "Pages integration" do
  let(:page) { FactoryGirl.create(:page }

  describe "Page Management tool" do

    describe "Visiting a page" do
      let(:user) { FactoryGirl.create(:user)}
      it "Should see the welcome message" do
        sign_in_user(admin_user)
        should have_content("Welcome")
      end
    end
  end
end

和我的档案test/support/utilities.rb

def sign_in_user(user)
  visit root_path
  fill_in "EMAIL ADDRESS",    with: user.email
  fill_in "PASSWORD",         with: user.password
  click_button "SIGN IN"
end

这是我的minitest.rb ...以防万一我可能做错了什么

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)

require "minitest/autorun"
require "minitest/rails"
require "minitest/rails/capybara"
require "active_support/testing/setup_and_teardown"
require 'turn'

require 'factory_girl'

Dir[Rails.root.join("test/support/**/*.rb")].each {|f| require f}

Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory|
  require factory
end

require 'database_cleaner'
DatabaseCleaner.strategy = :truncation

class MiniTest::Rails::ActiveSupport::TestCase
  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
  end
end

class IntegrationTest < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL

  register_spec_type(/integration$/, self)

  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end
end

class HelperTest < MiniTest::Spec
  include ActiveSupport::Testing::SetupAndTeardown
  include ActionView::TestCase::Behavior
  register_spec_type(/Helper$/, self)

  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
  end
end

关于什么是问题的任何想法......

1 个答案:

答案 0 :(得分:2)

最新版本的Capybara(2.0分支)有需要考虑的变化。以下是changelog的相关部分(相关段落加粗):

  

值得注意的是,我们更改了Capybara假设您的规格运行的类型   在RSpec中:功能(以前是:请求)。最新的   发布规范/功能。或者您可以使用Capybara   功能DSL(功能而不是描述),应该没有   任何额外的调整。 如果您看到未定义方法等错误   访问,然后你可能遇到这个问题。如果你是   包括模块:请求规格,你可能需要   将其更改为:feature。

因此,可以将describe更改为feature来纠正问题。