1

我是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
4

2 回答 2

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
于 2014-08-08T04:27:07.277 回答
0

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

顺便问一下,你为什么config.use_transactional_fixtures禁用了?如果您坚持使用before(:each)钩子,RSpec 将为您重置数据库,您不需要使用DatabaseCleaner.

于 2014-08-07T11:42:51.127 回答