【问题标题】:Object is created. (RoR, Database Cleaner, Factory Girl)对象被创建。 (RoR,数据库清洁工,工厂女孩)
【发布时间】:2014-08-07 11:24:17
【问题描述】:

我是 Rails 的初学者,帮帮我,我不明白问题是什么。好像之前写了别的东西(:每个)...... 以及如何在 test_spac.rb 中编写一个没有创建 Card 对象的测试。 before(:each) 方法不适用于此测试。

这是工作:

test_spec.rb:

require 'rails_helper'

describe "Index Page" do

  before(:each) do
    @card = FactoryGirl.create(:card)
    DatabaseCleaner.start
  end

  it "shows error messages if translation is wrong" do
    visit root_path
    fill_in 'translated_text', with: 'wqww'
    click_on 'Проверить'
    expect(page).to have_content('Неверно!')
  end

  after(:each) do
    DatabaseCleaner.clean
  end

end

rails_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'database_cleaner'
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :poltergeist


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

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

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

  config.use_transactional_fixtures = false

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each, type: :features) do
    DatabaseCleaner.strategy = :truncation
  end

  config.infer_spec_type_from_file_location!
end

这是行不通的:

test_spec.rb:

require 'rails_helper'

describe "Index Page" do

  before(:each) do
    @card = FactoryGirl.create(:card)
  end

  it "shows error messages if translation is wrong" do
    visit root_path
    fill_in 'translated_text', with: 'wqww'
    click_on 'Проверить'
    expect(page).to have_content('Неверно!')
  end

end

rails_helper.rb:

config.use_transactional_fixtures = false

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each, type: :features) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
        DatabaseCleaner.start
      end

  config.after(:each) do
        DatabaseCleaner.clean
      end

【问题讨论】:

    标签: ruby-on-rails database-cleaner


    【解决方案1】:

    问题解决了:

    我做这个:(test_spec.rb) 需要'rails_helper'

    describe "Index Page" do
    
      it "check behavior then translation is wrong" do
        FactoryGirl.create(:card) #################
        visit root_path
        fill_in 'translated_text', with: 'some text'
        click_on 'Проверить'
        expect(page).to have_content('Неверно!')
      end
    
    
      it "check page then object is not created" do
        visit root_path
        expect(page).to have_content('Непросмотренных')
      end
    
    end
    

    【讨论】:

      【解决方案2】:

      你能发布你所说的不工作的意思吗?您是否收到任何错误消息?

      顺便问一下,你为什么禁用config.use_transactional_fixtures?如果你坚持使用before(:each) 钩子,RSpec 将使用reset the database for you,你不需要使用DatabaseCleaner

      【讨论】:

      • Capybara::ElementNotFound: 无法找到字段 'translated_text' 页面加载时没有表单。 (因为只有在创建对象时才加载表单)
      • 之所以禁用config.use_transactional_fixtures是因为我以后需要管理数据库策略。
      • @VladimirFomin 您是否在不使用DatabaseCleaner 时尝试过第二个(非工作)版本?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多