首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rspec-rails用FactoryGirl发布单个param

rspec-rails用FactoryGirl发布单个param
EN

Stack Overflow用户
提问于 2015-01-17 01:23:17
回答 1查看 76关注 0票数 0

我有FavoritesController#create,它只接受来自post请求的一个输入param,doctor_id

代码语言:javascript
复制
class FavoritesController < ApplicationController
  before_action :authenticate_user!
  def create
    @favorite = Favorite.new(favorite_params)
    @favorite.patient_id = current_user.id

    respond_to do |format|
      if @favorite.save!
        format.js
      else
        format.json { render json: @favorite.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def favorite_params
      params.require(:favorite).permit(:doctor_id)
    end
end

我有两个例子来测试它

代码语言:javascript
复制
describe FavoritesController do

  login_patient

  def valid_attributes
    FactoryGirl.attributes_for(:favorite, doctor_id: rand(1..1000))
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Favorite" do
        expect {
          post :create, favorite: valid_attributes, format: :js
        }.to change(Favorite, :count).by(1)
      end

      it "redirects to the created favorite" do
        post :create, favorite: valid_attributes, format: :js
        response.should render_template :create
      end
    end
  end
end

但我在参数上有错误

代码语言:javascript
复制
1) FavoritesController POST create with valid params creates a new Favorite
   Failure/Error: post :create, favorite: valid_attributes, format: :js
   ActiveRecord::RecordInvalid:
     Validation failed: Doctor can't be empty
   # ./app/controllers/favorites_controller.rb:18:in `block in create'
   # ./app/controllers/favorites_controller.rb:17:in `create'
   # ./spec/controllers/favorite_controller_spec.rb:17:in `block (5 levels) in <top (required)>'
   # ./spec/controllers/favorite_controller_spec.rb:16:in `block (4 levels) in <top (required)>'

2) FavoritesController POST create with valid params redirects to the created favorite
   Failure/Error: post :create, favorite: valid_attributes, format: :js
   ActiveRecord::RecordInvalid:
     Validation failed: Doctor can't be empty
   # ./app/controllers/favorites_controller.rb:18:in `block in create'
   # ./app/controllers/favorites_controller.rb:17:in `create'
   # ./spec/controllers/favorite_controller_spec.rb:22:in `block (4 levels) in <top (required)>'

我相信我已经给出了控制器中定义的有效参数,但是由于验证,它似乎是在请求@doctor而不是doctor_id

代码语言:javascript
复制
class Favorite < ActiveRecord::Base
  belongs_to :doctor
  belongs_to :patient

  validates :doctor, :patient, presence: true
end

这是我的工厂

代码语言:javascript
复制
require 'faker'

FactoryGirl.define do
  factory :user do
    name = Faker::Name.name
    name name
    address Faker::Address.street_address
    phone Faker::PhoneNumber.phone_number
    password Faker::Internet.password
    sequence(:email) { |n| "#{name.split.join.downcase}_#{n}@example.com" }
  end

  factory :doctor, class: Doctor, parent: :user do
    roles [0,1]
    field
  end

  factory :patient, class: Patient, parent: :user do
    roles [1]
  end
end

有办法解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-17 10:53:55

代码语言:javascript
复制
class Favorite < ActiveRecord::Base
  belongs_to :doctor
  belongs_to :patient

  validates :doctor, :patient, presence: true
end

作为Favorite is belongs to doctor,当你试图创建最喜欢的医生之前应该存在。

当您试图创建favorite时,您将得到此错误,它查找在DB中传递的带有id的存在doctor,并将random number作为doctor_id传递,因此它将fails validation作为doctor record does not exist

您必须首先create doctor record,并将医生记录的id传递给最喜爱属性的doctor_id

备选案文1:-

代码语言:javascript
复制
  def valid_attributes
    doctor = FactoryGirl.create(:doctor)
    FactoryGirl.attributes_for(:favorite, doctor_id: doctor.id)
  end

备选案文2 :-

代码语言:javascript
复制
  def valid_attributes(doctor_id)
    FactoryGirl.attributes_for(:favorite, doctor_id: doctor_id)
  end

  let(:doctor) { create(:doctor) }
  describe "POST create" do
    describe "with valid params" do
      it "creates a new Favorite" do
        expect {
          post :create, favorite: valid_attributes(doctor.id), format: :js
        }.to change(Favorite, :count).by(1)
      end
    end
  end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27995201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档