【问题标题】:Is this a conflict in RoR tutorial test?这是 RoR 教程测试中的冲突吗?
【发布时间】:2014-08-08 18:22:59
【问题描述】:

我正在关注 Michael Hartl 的 Ruby on Rails 教程。

在他的清单 9.42 中,他在索引页面上展示了一个删除用户链接的测试。Ž 测试应该确保具有admin: true 属性的用户在用户的索引页面(用户的列表页面)上看到delete 链接。

另外,管理员(第一个用户)不应该看到删除自己的链接。

测试代码如下:

describe "delete links" do
  it { should_not have_link('delete') }

  describe "as an admin user" do
    let(:admin) { FactoryGirl.create(:admin) }
    before do
      sign_in admin
      visit users_path
    end

    it { should have_link('delete', href: user_path(User.first)) }
    it "should be able to delete another user" do
      expect do
        click_link('delete', match: :first)
      end.to change(User, :count).by(-1)
    end
    it { should_not have_link('delete', href: user_path(admin)) }
  end
end

这段代码让我感到困惑的是: 在我看来,describe 块中的第一个 it 子句似乎是合乎逻辑的 其中提到了 User.first 的路径(这里是 admin,因为 admin 在数据库中是第一位的) 与 describe 块中的第三个 it 子句冲突,这需要 指向管理员删除的链接不存在。

我错过了什么吗?

我什至还没有运行这个东西,但在我看来它必须失败。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rspec capybara railstutorial.org


    【解决方案1】:

    不是在您的问题中显示,而是第一个 it 块没有冲突,因为第一个用户是在此代码上方创建的。

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

    第三个 it 块是指当管理员签名时,它不应该有自己的删除按钮。

    这是#index 的完整规范:

    subject { page }
    
    describe "index" do
    
      let(:user) { FactoryGirl.create(:user) } #First User Created Here 
    
      before do
        sign_in user
        visit users_path
      end
    
      it { should have_title('All users') }
      it { should have_content('All users') }
    
      describe "pagination" do
        .
        .
        .
      end
    
      describe "delete links" do
    
        it { should_not have_link('delete') }
    
        describe "as an admin user" do
          let(:admin) { FactoryGirl.create(:admin) } #Admin Created here
          before do
            sign_in admin
            visit users_path
          end
    
          it { should have_link('delete', href: user_path(User.first)) }
          it "should be able to delete another user" do
            expect do
              click_link('delete', match: :first)
            end.to change(User, :count).by(-1)
          end
          it { should_not have_link('delete', href: user_path(admin)) }
        end
      end
    end
    

    【讨论】:

      【解决方案2】:

      当然,您应该运行它以找出实际情况。根据我的理解,这取决于您的 database 清洁器的工作方式,如果它在每次运行测试时清洁数据库(这是常见情况),那么上述规范将通过。

      【讨论】:

        猜你喜欢
        • 2014-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-06
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        相关资源
        最近更新 更多