【问题标题】:Rspec + FactoryGirl : Why does the placement of 'let' statements affect creation of variable and usage in testRspec + FactoryGirl:为什么'let'语句的放置会影响变量的创建和测试中的使用
【发布时间】:2017-11-06 08:18:33
【问题描述】:

我很难理解创建数据以在测试中使用的 Rspec 逻辑。

我有几种情况,除了第一种情况外,所有情况都会导致错误。错误意味着当我打印页面时,记录没有在 HTML 中呈现,这意味着没有创建变量。

这是我的工厂:

文章:

FactoryGirl.define do   
  factory :article do
    title "First test article"
    summary "Summary of first article"
    description "This is the first test article."
    user
  end  
end

评论:

FactoryGirl.define do
  factory :comment do
    sequence(:content) { |n| "comment text #{n}" }
    article
    user
  end
end

spec/features/article_spec.rb

1) 在 rspec 测试中显式创建注释变量。

require 'rails_helper'

describe "Comments on article" do

let!(:user) { FactoryGirl.create(:user) }
let!(:article) { FactoryGirl.create(:article) }
let!(:comment) {Comment.create(content:"Some comments", article_id: article.id, user_id: user.id)}

before do
    login_as(user, :scope => :user)
    visit article_path(article)
end

describe 'edit', js: true do

let!(:comment) {Comment.create(content:"Some comments", article_id: article.id, user_id: user.id)}

  it 'a comment can be edited through ajax' do
    print page.html
    find("a[href = '/articles/#{article.friendly_id}/comments/#{comment.id}/edit']").click
    expect(page).to have_css('#comment-content', text: "Some comments")
    within('.card-block') do
       fill_in 'comment[content]', with: "Edited comments"
       click_on "Save"
    end
    expect(page).to have_css('#comment-content', text: "Edited comments")
  end

end

结束

2) 替换 let!(:comment) {Comment.create(content:"Some comments", article_id: article.id, user_id: user.id)} 如下:

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

3) 放置让!第一个“it”块之前的语句

describe 'edit', js: true do

let!(:comment) {Comment.create(content:"Some comments", article_id: article.id, user_id: user.id)}

  it 'a comment can be edited through ajax' do
    print page.html
    find("a[href = '/articles/#{article.friendly_id}/comments/#{comment.id}/edit']").click
    expect(page).to have_css('#comment-content', text: "Some comments")
    within('.card-block') do
       fill_in 'comment[content]', with: "Edited comments"
       click_on "Save"
    end
    expect(page).to have_css('#comment-content', text: "Edited comments")
  end

end

更新:

我发现设置测试数据/变量对我作为 Rspec 新手来说是一个很大的痛苦/绊脚石。以下是我发现的一些对我有帮助的参考资料:

1) When is using instance variables more advantageous than using let()?

2) https://www.ombulabs.com/blog/rails/rspec/ruby/let-vs-instance.html

【问题讨论】:

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


    【解决方案1】:
    let!(:comment) {Comment.create(content:"Some comments", article_id: article.id, user_id: user.id)}
    
    let!(:comment) { FactoryGirl.create(:comment) }
    

    这些不会创建相同的 cmets - 在第二种情况下,创建的评论不会与用户或文章相关联(但新用户和新文章)。

    您是在检查变量并且它是 nil,还是只是按照您在页面上看到的内容?如果您按照页面上的内容进行操作,是否可能仅向您显示与给定文章/用户关联的 cmets?

    至于另一个问题,before 块在 describe 中创建的评论变量之前执行 - 所以评论在您导航到页面之前不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 2014-08-12
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      相关资源
      最近更新 更多