Rspec:由与登录用户不同的用户创建的注释

时间:2017-11-06 19:11:56

标签: rspec capybara factory-bot

我是rspec和capybara的新手,并通过慢慢扩展我的测试场景来学习。

我无法理解为什么登录后创建的评论(例如朋友)会被另一个测试用户(例如Test1)打印出来?

(我已在浏览器中检查并且评论显示为由当前用户创建)

require 'rails_helper'

describe 'navigate' do

  let!(:user) { FactoryGirl.create(:user) }
  let!(:friend) { FactoryGirl.create(:friend) }
  let!(:article) {
    Article.create(title:"To test notifications", summary: "Summary of article", description: "Description of article.", status: "published", user_id: user.id)
  }

  describe 'notification created if other use comments on article' , js: true do

    before do
      login_as(friend, :scope => :user)
      visit article_path(article)
        fill_in 'comment[content]', with: "Some comments"
      click_on "Add Comment"
      logout(:friend)
      login_as(user, :scope => :user)
    end

    it "renders notification on dropdown list" do
      expect(page).to have_css("#notificationList", text: friend.name + " posted a Comment on " + article.title)
      expect(page).to have_css("#notification-counter", text: "1")
    end

    it "render notification on notifiction index page" do
      visit notifications_path
      expect(page).to have_css("#notifications-container", text: friend.name + " posted a Comment on " + article.title)
    end

  end

end

我也尝试过以下方法,但测试失败,因为我无法登录该应用程序。

2)将login_as(friend, :scope => :user)替换为login_as(friend, :scope => :friend)

用户工厂:

FactoryGirl.define do
  sequence :email do |n|
    "test#{n}@example.com"
  end

  factory :user do
    sequence(:name) { |n| "Test#{n}" }
    email { generate :email }
    password "asdfasdf"
    password_confirmation "asdfasdf"
  end

  factory :friend, class: "User" do
    name 'Friend'
    email { generate :email }
    password "asdfasdf"
    password_confirmation "asdfasdf"
  end

end

1 个答案:

答案 0 :(得分:0)

假设您的控制器/视图中没有出现逻辑错误(它在开发模式下工作),这很可能是因为您没有等待测试中的操作完成。最简单的测试方法是将sleep 2放在click_onlogout之后。如果这可以解决问题,那么正确的解决方案是为操作完成时出现的内容添加期望

before do
  login_as(friend, :scope => :user)
  visit article_path(article)
  fill_in 'comment[content]', with: "Some comments"
  click_on "Add Comment"
  expect(page).to have_text "Comment Added" # whatever is shown once "Add Comment" succeeds
  logout(:friend)
  expect(page).to have_text "You have logged out" # whatever change indicates logout has completed
  login_as(user, :scope => :user)
end

这是因为click_on无法保证等待通过点击触发的任何操作(无法知道这些操作可能是什么)。在你的情况下,这意味着"添加评论"点击,紧接着logout发生的任何事情,然后你致电login_aslogin_as通过告诉warden / devise中间件如同指定的用户登录进行下一个请求一样工作,所以如果在请求发生之前发生了这样的话,那就是"添加注释"点击进行处理它可以为"添加评论"要求处理,就好像user而不是friend。通过添加睡眠或期望,您可以为正确的用户提供操作时间,并确保在完成测试之前已完成。

此外,login_as提供的设计/监护人实际上并未访问某个网页,因此,如果您使用login_as的版本,则可能需要visit <somewhere> }在第一次测试开始时,一个页面被访问为user

最后一点,你的工厂中有很多重复 - 你最好把它写成

FactoryGirl.define do
  sequence :email do |n|
    "test#{n}@example.com"
  end

  factory :user do
    sequence(:name) { |n| "Test#{n}" }
    email { generate :email }
    password "asdfasdf"
    password_confirmation "asdfasdf"

    factory :friend do
      name 'Friend'
    end
  end
end

https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#inheritance