【发布时间】:2015-04-30 00:43:43
【问题描述】:
所以我有一个User 那个has_many :comments。
在我的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工厂:
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
想法?
【问题讨论】:
-
您是否在测试中存根登录过程? more details here
-
我没有。感谢您的提示。
标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails rspec3