Итак, у меня есть User
, что has_many :comments
.Как проверить комментарии current_user в RSpec?
В моей Comments#Index
, у меня есть это:
def index
@comments = current_user.comments
end
Внутри моего Rspec.config...
блока в rails_helper.rb
у меня есть это:
# Add Config info for Devise
config.include Devise::TestHelpers, type: :controller
Мои comments_controller_spec.rb
выглядит следующим образом:
describe 'GET #index' do
it "populates an array of comments that belong to a user" do
user = create(:user)
node = create(:node)
comment1 = create(:comment, node: node, user: user)
comment2 = create(:comment, node: node, user: user)
get :index, { node_id: node }
expect(assigns(:comments)).to match_array([comment1, comment2])
end
it "renders the :index template"
end
Этот это мой Users.rb
f actory:
FactoryGirl.define do
factory :user do
association :family_tree
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password "password123"
password_confirmation "password123"
bio { Faker::Lorem.paragraph }
invitation_relation { Faker::Lorem.word }
# required if the Devise Confirmable module is used
confirmed_at Time.now
gender 1
end
end
Это мой Comments
завод:
FactoryGirl.define do
factory :comment do
association :node
message { Faker::Lorem.sentence }
factory :invalid_comment do
message nil
end
end
end
Это ошибка я получаю в настоящее время:
Failure/Error: get :index, { node_id: node }
NoMethodError:
undefined method `comments' for nil:NilClass
Мысли?
Вы заглушили входной сигнал в вашем тесте? [подробнее] [https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs) – AbM
Я этого не делал. Спасибо за совет. – marcamillion