2

我正在使用 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

关于问题的任何想法......

4

1 回答 1

2

Capybara 的最新版本(2.0 分支)有一些需要考虑的变化。这是变更日志中的相关部分(相关段落以粗体显示):

值得注意的是,我们将 Capybara 假设您的规范在 RSpec 中运行的 :type 更改为 :feature (以前是 :request)。规格/功能的最新版本。或者,您可以使用 Capybara Feature DSL(功能而不是描述),它应该可以在没有任何额外调整的情况下工作。如果您看到诸如未定义方法访问之类的错误,那么您可能遇到了这个问题。如果您将模块包含在 :request 规范中,您可能需要将其更改为 :feature。

因此,有可能将其更改describefeature可以纠正问题。

于 2012-12-20T14:57:43.177 回答