FactoryGirl - 用户/列表混乱

时间:2015-11-12 13:32:38

标签: ruby-on-rails-4 rspec tdd factory-bot

我花了3天的时间尝试重构我的rspec测试以合并FactoryGirl,觉得我需要求助于SO。

我有以下内容:

工厂/ listings.rb

 FactoryGirl.define do
   factory :listing do

     title { Faker::Lorem.sentence }
     subtitle { Faker::Lorem.sentence }
     description { Faker::Lorem.sentence }
     price { Faker::Commerce.price }
     condition { "New" }
     brand { Faker::Company.name }
     model { Faker::Commerce.product_name }
     case_type { "No Case" }
     location { Faker::Address.city }
 end

 trait :les_paul do
   title "1959 Les Paul"
   subtitle "A true gem with OHSC"
   description "Test description"
 end

我也有工厂/ users.rb

FactoryGirl.define do
  factory :user do
  first_name "Ben"
  last_name "Smith"
  user_name "bensmith"
  password "password"
  password_confirmation "password"
  email "ben@test.com"
end

trait :bob do
  email "bob@test.com"
end

然后我在features / listings_feature_spec.rb中进行了我的功能测试:

feature 'listings' do
  let(:user) { FactoryGirl.create(:user) }
  let!(:category) { FactoryGirl.create(:category) }
  let!(:listing) { FactoryGirl.create(:listing, :les_paul, user: user, category: category) }
  let!(:user_two) { FactoryGirl.create(:user, :bob) }

  ## Lots of passing tests then...

  context 'deleting listings' do
    before { login_as(user) }

    scenario 'removes a listing when the listing owner clicks a delete link' do
      visit listings_path
      click_link '1959 Les Paul'
      expect(current_path).to eq listing_path(listing)
      save_and_open_page
      click_link 'Delete 1959 Les Paul'
      expect(page).not_to have_content '1959 Les Paul'
      expect(current_path).to eq listings_path
      expect(page).to have_content 'Your listing was successfully deleted'
    end

由于视图listing / show.html.erb中的条件:

,此测试失败
 <% if @listing.user == current_user %>
   <%= link_to "Edit #{@listing.title}", edit_listing_path %>
   <%= link_to "Delete #{@listing.title}", listing_path(@listing), method: :delete %>
 <% end %>

这是测试失败......

 2) listings deleting listings removes a listing when the listing owner clicks a delete link
 Failure/Error: click_link 'Delete 1959 Les Paul'
 Capybara::ElementNotFound:
 Unable to find link "Delete 1959 Les Paul"
 # /Users/benhawker/.rvm/gems/ruby-2.1.5/gems/capybara-2.5.0/lib/capybara/node/finders.rb:43:in `block in find'
 # /Users/benhawker/.rvm/gems/ruby-2.1.5/gems/capybara-2.5.0/lib/capybara/node/base.rb:84:in `synchronize'
 # /Users/benhawker/.rvm/gems/ruby-2.1.5/gems/capybara-2.5.0/lib/capybara/node/finders.rb:32:in `find'
 # /Users/benhawker/.rvm/gems/ruby-2.1.5/gems/capybara-2.5.0/lib/capybara/node/actions.rb:26:in `click_link'
 # /Users/benhawker/.rvm/gems/ruby-2.1.5/gems/capybara-2.5.0/lib/capybara/session.rb:698:in `block (2 levels) in <class:Session>'
 # /Users/benhawker/.rvm/gems/ruby-2.1.5/gems/capybara-2.5.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
 # ./spec/features/listings_feature_spec.rb:138:in `block (3 levels) in <top (required)>'

我的主要问题: 1 - 在此测试中,我可以看到创建列表的用户与登录的用户不同。我可以验证这一点,因为当我使用save_and_open_page方法运行测试时,我可以看到对2个不同user_id的引用。我在这里遗失了一些东西,看不到它。

2 - 我不应该在listing_feature_spec中创建用户和类别,然后将其传递给let!(:listing)。在之前的提交中,我正在与我的工厂内的关联一起工作,但这导致所有测试都失败并出现其他错误。我想了解如何重构工厂来干掉我的代码。

我在这里添加了工厂链接 - https://github.com/benhawker/rails_marketplace/tree/master/spec/factories

0 个答案:

没有答案