【问题标题】:Capybara with rspec not working带有 rspec 的水豚不起作用
【发布时间】:2013-10-11 00:41:34
【问题描述】:

您好,我正在尝试通过 capybara 填写一些数据来测试表格。我的测试运行没有任何错误,但我在测试或开发数据库中都看不到该数据。我的测试在“规范/功能/文件名”中。我的测试类似于

require 'spec_helper'

  feature "New Application" do
    scenario 'has 200 status code if logged in' do 
    visit '/applications/new?id=.........'
    fill_in 'application[applicants_attributes][0][first_name]', :with => 'Rose'
    fill_in 'application[applicants_attributes][0][first_name]', :with => 'Farmer'
    click_link 'sbmt'
    current_path.should == '/applications/new'
    page.status_code.should be 200
    end    
  end

需要帮助!!!! 我的 spec_helper 类似于

require 'simplecov'
SimpleCov.start do
  add_filter '/spec/'
  add_filter '/config/'
  add_filter '/lib/'
  add_filter '/vendor/'
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

# Add this to load Capybara integration:
require 'capybara/rspec'
require 'capybara/rails'
require 'rspec/autorun'
require 'crack'

Capybara.register_driver :rack_test do |app|
  Capybara::RackTest::Driver.new(app, :headers => { 'User-Agent' => 'Capybara' })
end
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  config.include Capybara::DSL, type: :feature


  RSpec.configure do |config|
    config.use_transactional_fixtures = false
  #config.use_transactional_fixtures = false

  config.before :each do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

我的 rspec 输出是

Failures:

  1) New Application has 200 status code if logged in
     Failure/Error: Application.where("first_name = 'Rose' AND last_name = 'Farmer'").count.should == 1
       expected: 1
            got: 0 (using ==)
     # ./spec/features/applications_controller_spec.rb:23:in `block (2 levels) in <top (required)>'

Finished in 0.7381 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/applications_controller_spec.rb:4 # New Application has 200 status code if logged in

Randomized with seed 49732

Coverage report generated for Cucumber Features to /home/nomi/homesbyhaven/coverage. 241 / 616 LOC (39.12%) covered.

问题是我无法将数据保存到数据库。如果我从测试中删除对应用程序表中数据的检查。它通过了。但我如何验证它是否真的通过了。我的意思是说没有问题。

谢谢

【问题讨论】:

  • 您是否启用了 transactional_fixtures?如果是这样,您的所有事务都将在测试结束时回滚
  • 我已尝试禁用它们,但我得到了“#<:core::configuration:0x967f288> (NoMethodError) 的未定义方法 `use_transactional_fixtures='”
  • 发布您的 rspec 输出!!!我们不知道您的问题的症状是什么。您看不到数据库中的数据还是只是您的测试失败了?
  • @Kosmonaut 我已经更新了 rspec 输出。如果我在测试中检查了数据库中的数据,问题是测试没有通过。
  • @Kosmonaut 如果我删除了检查,那么测试就通过了,但是我怎么能确定他们没有错

标签: ruby-on-rails rspec capybara


【解决方案1】:

我认为这里的问题是您正在测试错误的东西。功能(集成)规范测试用户与系统交互的方式。因此,您的测试应尽可能限制用户可以执行的活动。例如,在添加用户后,User.count 增加了 1,而不是检查,而是让测试执行与用户相同的操作来验证操作是否成功。您可以访问用户页面以查看用户已添加,或者在页面上添加一个元素来告诉您存在多少用户。

请记住,测试服务器和浏览器使用不同的进程。这可能会导致时间问题,您希望服务器在一个进程中完成了一项操作(例如添加用户),但数据库更改正在另一个进程中发生。您可以通过让测试浏览器模仿用户的操作来避免这些问题。在模型规范中进行数据库测试。

【讨论】:

    【解决方案2】:

    默认情况下,每次测试后都会清除测试数据库。这样每次运行测试时,您都会得到一张白纸。

    如果您不希望这种情况发生,您可以将您的 spec/spec_helper.rb 中的配置更改为

    config.use_transactional_fixtures = false
    

    但是,如果您在每次测试后不清除数据库,那么从先前运行的测试创建的任何记录都可能会更改下一次测试的结果。给你一个误报或误报

    【讨论】:

    • 感谢您的回答。但我得到了#<:core::configuration:0x967f288> (NoMethodError) 的未定义方法 `use_transactional_fixtures=' 并且我在数据库中没有记录。
    【解决方案3】:

    您将在您的 spec/spec_helper.rb 中有一个如下所示的块。我不建议禁用事务性固定装置,因为当您进行多个测试时,它会导致很多问题。

    RSpec.configure do |config|
        # Mock Framework
        config.mock_with :rspec
    
        # If you're not using ActiveRecord, or you'd prefer not to run each of your
        # examples within a transaction, remove the following line or assign false
        # instead of true.
        config.use_transactional_fixtures = true
    
        # Render views when testing controllers
        # This gives us some coverages of views
        # even when we aren't testing them in isolation
        config.render_views
      end
    

    您可以做的是将数据库检查作为测试用例中的断言

      require 'spec_helper'
    
      feature "New Application" do
        scenario 'has 200 status code if logged in' do 
          visit '/applications/new?id=.........'
          fill_in 'application[applicants_attributes][0][first_name]', :with => 'Rose'
          fill_in 'application[applicants_attributes][0][last_name]', :with => 'Farmer'
          click_link 'sbmt'
          current_path.should == '/applications/new'
          Application.where("first_name = 'Rose' AND last_name = 'Farmer'").count.should == 1
          page.status_code.should be 200
        end    
      end
    

    如果上述测试失败,那么您的功能没有按预期工作

    如果您使用 ActiveRecord 以外的任何东西,则不能使用事务性固定装置。 如果您使用的是 MongoID,则可以将 Database Cleaner 与截断策略一起使用

    RSpec.configure do |config|
      config.use_transactional_fixtures = false
    
      config.before :each do
        DatabaseCleaner.strategy = :truncation
        DatabaseCleaner.start
      end
    
      config.after do
        DatabaseCleaner.clean
      end
    end
    

    【讨论】:

    • 我仍然收到“未定义的 transactional_fixtures 方法”:( 但如果我删除该行,则测试失败
    • 你之前是如何通过考试的?
    • 你没有使用 ActiveRecord 吗?
    • 我正在使用 mongoid。有没有兼容性问题??
    • 测试再次失败:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多