首页 文章

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

提问于
浏览
1

我已经运行了测试,似乎用户不是由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 回答

  • 3

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

相关问题