RSpec Rails NoMethodError:用户的未定义方法`password ='

时间:2017-01-18 16:09:52

标签: ruby-on-rails rspec

我已经运行了测试,并且看起来用户不会被Factory Girl创建。这就是我得到的:

reports_controller_spec.rb

require 'rails_helper'

RSpec.describe ReportsController do
  let(:user) { create :user }

  before { sign_in user }

  describe 'GET #subjects' do
    subject { get :subjects }

    it_behaves_like 'template rendering action', :subjects
  end
end

工厂/ users.rb的

FactoryGirl.define do
  factory :user do
    email { Faker::Internet.safe_email }
    password { Faker::Internet.password }
  end
end

当我运行测试时,我收到此错误:

ReportsController
  GET #subjects
    behaves like template rendering action
      example at ./spec/support/shared/template_rendering_action.rb:2 (FAILED - 1)

Failures:

  1) ReportsController GET #subjects behaves like template rendering action 
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `password=' for #<User:0x000000063abd20>
     Shared Example Group: "template rendering action" called from ./spec/controllers/reports_controller_spec.rb:11
...

我不明白为什么它不起作用。任何人都可以指出我的错误?感谢。

编辑:我正在使用设计。

1 个答案:

答案 0 :(得分:4)

如果您使用的是Devise,则需要将password_confirmation添加到:user工厂