【问题标题】:Capybara / Rspec / FactoryGirl spec failing when the code does work as intended当代码按预期工作时,Capybara / Rspec / FactoryGirl 规范失败
【发布时间】:2014-05-18 17:14:46
【问题描述】:

rspec 规范有问题。实际代码似乎工作正常,但我的规范仍然失败。我正在使用 FactoryGirl、Rspec 和 Capybara。这是 spec/features/admin/admin_users_spec.rb 的一部分:

require 'spec_helper'

describe "Admin::Users" do

  subject { page }

  context "When admin signed in" do
    let(:admin) { FactoryGirl.create(:admin) }
    let(:user) { FactoryGirl.create(:user) }

    before { sign_in admin }

    context "when visiting admin users" do

      before { visit admin_users_path }

      it_should_behave_like "admin visiting admin page"
      it { should have_content('- User Index') }
      it { should have_content(user.email) }  # This test is failing
    end
  end
end

失败的测试有注释。

这是我的用户工厂。

require 'faker'

FactoryGirl.define do 
  factory :user do
    email         { Faker::Internet.email }
    admin         false
    super_admin   false
    password      "password"
    password_confirmation "password"
    confirmed_at  Time.now

    factory (:admin) do
      admin true
    end

    factory (:super_admin) do 
      admin true
      super_admin true
    end
  end
end

当您转到管理仪表板中的用户索引时,管理员应该看到一个用户列表。因此,除了当前的管理员之外,我还创建了另一个用户来填充测试数据库。这是一个示例失败消息:

Failures:

  1) Admin::Users When admin signed in when visiting admin users should text "christian.stark@rempel.com"
     Failure/Error: it { should have_content(user.email) }
       expected to find text "christian.stark@rempel.com" in "Under The Gun Theater Admin Edit profile Logout Dashboard Users Posts Pages Shows Classes People Photos Admin Dashboard - User Index Email Roles Last Logged In geovanni.marks@kaulke.com admin May 18, 2014 11:59 Logged in as geovanni.marks@kaulke.com"
     # ./spec/features/admin/admin_users_spec.rb:53:in `block (4 levels) in <top (required)>' 

如您所见,页面上生成的表格仅包含管理员的电子邮件地址。未找到其他用户“christian.stark@rempel.com”。同样,当我实际登录应用程序时,它可以工作,所以我不确定为什么这个测试不起作用。

【问题讨论】:

    标签: ruby-on-rails rspec capybara factory-bot


    【解决方案1】:

    使用let! 而不是let 作为user

    let惰性评估,也就是说,user 直到被调用时才会存在。 let! 通过热切地评估块来帮助解决这个问题。

    let!(:user) { FactoryGirl.create(:user) }
    

    在此处阅读有关let and let! 的更多信息。

    或者,您可以在before 中手动将create 用户屏蔽为:

    before do
      @user = FactoryGirl.create(:user)
      sign_in admin
    end
    #corresponding test:
    it { should have_content(@user.email) }  # This test should pass
    

    【讨论】:

      【解决方案2】:

      在您加载页面之前,不会创建您的用户。 lets 在 RSpec 中很懒惰。用户记录在此行创建:

      it { should have_content(user.email) } 
      

      这总是会失败,因为在页面加载之前没有创建用户。您可以将let(:user) 更改为let!(:user),它将在测试前急切地运行它。不过,您可能需要考虑不同的结构,这有助于避免这些问题:

      admin = create(:user, :admin)
      user = create(:user)
      
      sign_in admin
      visit admin_users_path
      
      expect(page).to have_content('- User Index')
      expect(page).to have_content(user.email)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-16
        • 1970-01-01
        • 1970-01-01
        • 2015-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        相关资源
        最近更新 更多