使用工厂女孩关联的Rspec特征规范

时间:2014-09-15 00:37:48

标签: ruby-on-rails ruby rspec factory-bot database-cleaner

我正在尝试为需要编辑其帐户模型设置的用户测试功能规范。我是新手测试,所以不确定我的问题是由于我如何设置我的工厂女孩​​协会或我的数据库清洁配置有问题。

FactoryGirl.define do
  factory :user do
    first_name "John"
    last_name "Smith"
    sequence(:email) { |n| "John#{n}@example.com" }
    password "pw"
  end
end

FactoryGirl.define do
  factory :account do
    association :owner, factory: :user
    name "Account One"
  end
end

我的spec_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'shoulda/matchers'
require 'database_cleaner'

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

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|

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

  config.use_transactional_fixtures = false

  config.infer_base_class_for_anonymous_controllers = false

  config.order = 'random'

  config.include FactoryGirl::Syntax::Methods

  config.include Devise::TestHelpers, type: :controller

  OmniAuth.config.test_mode = true

  OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
    :provider => 'twitter',
    :uid => '12345'
  })

  OmniAuth.config.add_mock(:google, {:uid => '12345'})

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

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

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

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

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

end

我的规格/功能/ account_spec.rb

require 'spec_helper'

feature 'Account' do
  before :each do
    @account = create(:account)
    @user = @account.owner
    sign_in
  end

  scenario 'a signed in user updates the account settings' do
    expect(current_path).to eq edit_account_path(@account.id)
    expect(page).to have_content('Edit Account')
    fill_in 'Company Name', with: 'New XYZ Co.'
    click_button 'Update Account'
    expect(page).to have_content('Settings were successfully updated.')
  end

  scenario 'a signed in user receives an error message when deletes company name' do
    fill_in 'Company Name', with: nil
    click_button 'Update Account'
    expect(page).to have_content("Can't be blank")
  end

  def sign_in
    visit root_path
    click_link 'Sign in'
    fill_in 'Email', with: @user.email
    fill_in 'Password', with: @user.password
    click_button 'Sign in'
    click_link 'Account'
  end

end

如果我只运行一个规范,我会通过测试:

Account
  a signed in user updates the account settings
  a signed in user receives an error message when deletes company name

Finished in 1.71 seconds
2 examples, 0 failures

但是当我运行整个测试套件时,我会收到错误:

  1) Account a signed in user updates the account settings
     Failure/Error: expect(current_path).to eq edit_account_path(@account.id)

       expected: "/accounts/11/edit"
            got: "/accounts/33/edit"

       (compared using ==)
     # ./spec/features/account_spec.rb:11:in `block (2 levels) in <top (required)>'

Finished in 6.85 seconds
88 examples, 1 failure, 7 pending

Failed examples:

rspec ./spec/features/account_spec.rb:10 # Account a signed in user updates the account settings

0 个答案:

没有答案