I have difficulty understanding the Rspec logic of creating data to use in tests.
I have a couple of scenarios and all result in errors except for the first scenario. Error meaning that when i print the page, the record is not rendered in the HTML, meaning that the variable is not created.
Here are my factories:
article:
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
comment:
FactoryGirl.define do
factory :comment do
sequence(:content) { |n| "comment text #{n}" }
article
user
end
end
spec/features/article_spec.rb
1) Explicitly create the comment variable within the rspec test.
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
end
2) Replacing let!(:comment) {Comment.create(content:"Some comments", article_id: article.id, user_id: user.id)}
with below:
let!(:comment) { FactoryGirl.create(:comment) }
3) Place the let! statement before the first 'it' block
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
UPDATE:
I find setting up test data/variables to be a big pain/stumbling block for me as a Rspec newbie. Here are some references i found that helped me:
1) When is using instance variables more advantageous than using let()?
2) https://www.ombulabs.com/blog/rails/rspec/ruby/let-vs-instance.html