RSpec控制器测试无法通过POST创建

受代码启发

我的程序运行正常,但是我的RSpec控制器测试代码中有问题。我需要帮助对规范进行故障排除,这些规范是在运行脚手架生成器时创建的。存在三个“使用有效参数创建POST”失败:

POST create
with valid params
  creates a new Appointment (FAILED - 1)
  assigns a newly created appointment as @appointment (FAILED - 2)
  redirects to the created appointment (FAILED - 3)

Failures:

1) AppointmentsController POST create with valid params creates a new Appointment
 Failure/Error: expect {
   expected #count to have changed by 1, but was changed by 0
 # ./spec/controllers/appointments_controller_spec.rb:90:in `block (4 levels) in <top (required)>'

2) AppointmentsController POST create with valid params assigns a newly created appointment as @appointment
 Failure/Error: expect(assigns(:appointment)).to be_persisted
   expected `#<Appointment id: nil, member_id: nil, trainer_id: nil, created_at: nil, updated_at: nil, date: "2020-01-02", starts_at: "2000-01-01 08:00:00", ends_at: "2000-01-01 09:00:00">.persisted?` to return true, got false
 # ./spec/controllers/appointments_controller_spec.rb:99:in `block (4 levels) in <top (required)>'

3) AppointmentsController POST create with valid params redirects to the created appointment
 Failure/Error: expect(response).to redirect_to(Appointment.last)
   Expected response to be a <redirect>, but was <200>
 # ./spec/controllers/appointments_controller_spec.rb:104:in `block (4 levels) in <top (required)>'

在第二次失败中,我注意到约会ID,成员和培训师的值为零,尽管我有有效的工厂供成员和培训师使用。我的成员工厂和培训工厂的测试通过了,并且工作正常。我认为问题一定是由我在Controller规范中设置“有效属性”哈希值引起的,但我不知道这是怎么回事。为什么POST创建测试失败?我需要怎么做才能让他们通过?

这是约会控制器RSpec的代码:

require 'rails_helper'


RSpec.describe AppointmentsController, :type => :controller do

let(:valid_attributes) { {
'date' => '2020-01-02',
'starts_at' => '08:00:00',
'ends_at' => '09:00:00',    
'member' => FactoryGirl.build(:member),
'trainer' => FactoryGirl.build(:trainer) 
}

}

let(:invalid_attributes) { {
'date' => '2000-01-02',
'starts_at' => '06:00:00',
'ends_at' => '09:00:00',
'member' => FactoryGirl.build(:member),
'trainer' => FactoryGirl.build(:trainer)    
}
}

let(:valid_session) { {
'date' => '2020-12-30',
'starts_at' => '15:00:00',
'ends_at' => '17:00:00', 
'member' => FactoryGirl.build(:member),
'trainer' => FactoryGirl.build(:trainer) 
}
}

describe "GET index" do
it "assigns all appointments as @appointments" do
  appointment = Appointment.create! valid_attributes
  get :index, {}, valid_session
  expect(assigns(:appointments)).to eq([appointment])
end
end

describe "GET show" do
it "assigns the requested appointment as @appointment" do
  appointment = Appointment.create! valid_attributes
  get :show, {:id => appointment.to_param}, valid_session
  expect(assigns(:appointment)).to eq(appointment)
end
end

describe "GET new" do
it "assigns a new appointment as @appointment" do
  get :new, {}, valid_session
  expect(assigns(:appointment)).to be_a_new(Appointment)
end
end

describe "GET edit" do
it "assigns the requested appointment as @appointment" do
  appointment = Appointment.create! valid_attributes
  get :edit, {:id => appointment.to_param}, valid_session
  expect(assigns(:appointment)).to eq(appointment)
end
end

describe "POST create" do
describe "with valid params" do
  it "creates a new Appointment" do
    expect {
      post :create, {:appointment => valid_attributes}, valid_session
    }.to change(Appointment, :count).by(1)
    save_and_open_page
  end

  it "assigns a newly created appointment as @appointment" do
    post :create, {:appointment => valid_attributes}, valid_session
    expect(assigns(:appointment)).to be_a(Appointment)
    expect(assigns(:appointment)).to be_persisted        
  end

  it "redirects to the created appointment" do
    post :create, {:appointment => valid_attributes}, valid_session
    expect(response).to redirect_to(Appointment.last)        
  end
  end

describe "with invalid params" do
  it "assigns a newly created but unsaved appointment as @appointment" do
    post :create, {:appointment => invalid_attributes}, valid_session
    expect(assigns(:appointment)).to be_a_new(Appointment)
  end

  it "re-renders the 'new' template" do
    post :create, {:appointment => invalid_attributes}, valid_session
    expect(response).to render_template("new")
  end
 end
end

describe "PUT update" do
describe "with valid params" do
  let(:new_attributes) { {
    'date' => '2020-01-02',
    'starts_at' => '10:00:00',
    'ends_at' => '12:00:00',
    'member' => FactoryGirl.build(:member),
    'trainer' => FactoryGirl.build(:trainer) 
   }
  }

  let(:invalid_attributes) { {
    'date' => '2005-03-15',
    'starts_at' => '04:00:00',
    'ends_at' => '09:00:00',
    'member' => FactoryGirl.build(:member),
    'trainer' => FactoryGirl.build(:trainer)    
  }
}

  it "updates the requested appointment" do
    appointment = Appointment.create! valid_attributes
    put :update, {:id => appointment.to_param, :appointment => new_attributes}, valid_session
    appointment.reload
    expect(controller.notice).to eq('Appointment was successfully updated.')
  end

  it "assigns the requested appointment as @appointment" do
    appointment = Appointment.create! valid_attributes
    put :update, {:id => appointment.to_param, :appointment => valid_attributes}, valid_session
    expect(assigns(:appointment)).to eq(appointment)
  end

  it "redirects to the appointment" do
    appointment = Appointment.create! valid_attributes
    put :update, {:id => appointment.to_param, :appointment => valid_attributes}, valid_session
    expect(response).to redirect_to(appointment)
  end
end

describe "with invalid params" do
  it "assigns the appointment as @appointment" do
    appointment = Appointment.create! valid_attributes
    put :update, {:id => appointment.to_param, :appointment => invalid_attributes}, valid_session
    expect(assigns(:appointment)).to eq(appointment)
  end

  it "re-renders the 'edit' template" do
    appointment = Appointment.create! valid_attributes
    put :update, {:id => appointment.to_param, :appointment => invalid_attributes}, valid_session
    expect(response).to render_template("edit")
    end
  end
 end

describe "DELETE destroy" do
 it "destroys the requested appointment" do
  appointment = Appointment.create! valid_attributes
  expect {
    delete :destroy, {:id => appointment.to_param}, valid_session
  }.to change(Appointment, :count).by(-1)
end

it "redirects to the appointments list" do
  appointment = Appointment.create! valid_attributes
  delete :destroy, {:id => appointment.to_param}, valid_session
  expect(response).to redirect_to(appointments_url)
  end
 end
end

这是约会控制器的代码:

class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update, :destroy]

# GET /appointments
# GET /appointments.json
def index
@appointments = Appointment.all
end

# GET /appointments/1
# GET /appointments/1.json
def show
end

# GET /appointments/new
def new
@appointment = Appointment.new
end

# GET /appointments/1/edit
def edit

结尾

# POST /appointments
# POST /appointments.json
def create
@appointment = Appointment.new(appointment_params)

respond_to do |format|
  if @appointment.save
    format.html { redirect_to @appointment, notice: 'Appointment was successfully created.' }
    format.json { render :show, status: :created, location: @appointment }
  else
    format.html { render :new }
    format.json { render json: @appointment.errors, status: :unprocessable_entity }
  end
 end
end

# PATCH/PUT /appointments/1
# PATCH/PUT /appointments/1.json
def update
respond_to do |format|
  if @appointment.update(appointment_params)
    format.html { redirect_to @appointment, notice: 'Appointment was successfully updated.' }
    format.json { render :show, status: :ok, location: @appointment }
  else
    format.html { render :edit }
    format.json { render json: @appointment.errors, status: :unprocessable_entity }
  end
 end
end

# DELETE /appointments/1
# DELETE /appointments/1.json
def destroy
@appointment.destroy
respond_to do |format|
  format.html { redirect_to appointments_url, notice: 'Appointment was successfully destroyed.' }
  format.json { head :no_content }
 end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
  @appointment = Appointment.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
  params.require(:appointment).permit(:date, :starts_at, :ends_at, :member_id, :trainer_id)
end
end

约会模型的代码:

class Appointment < ActiveRecord::Base

belongs_to :member
belongs_to :trainer

validates_date :date, :on => :create, :on_or_after => :today

validates_time :starts_at, :between => ['06:30', '21:00']
validates_time :starts_at, :after => :now, :if => :today_appointment #scopes validation to current day only  
validates_time :ends_at, :after => :starts_at

validate :duration_of_appointment

validates :member, :trainer, presence: true

validates :starts_at, :ends_at, :overlap => {
:exclude_edges => ["starts_at", "ends_at"],
:scope => "date",
:scope => "starts_at",    
:scope => "trainer_id"    
}

validates :starts_at, :ends_at, :overlap => {
:exclude_edges => ["starts_at", "ends_at"],
:scope => "member_id"
}    


private

def today_appointment
Date.current == self.date     
end  


def duration_of_appointment
length = (ends_at - starts_at) / 60
return if length.between?(30, 120) # stops validation if length falls between the two integers
errors.add(:base, 'Duration must be between 30 and 120 minutes')
end
end
费利克斯·博尔兹克(Felix Borzik)

您不应该向控制器提供成员和培训器的内置实例。相反,创建成员和教练,并通过他们的ID作为member_idtrainer_id在valid_attributes哈希值。首先,创建所需的培训师和成员:

before :each do
  @trainer = FactoryGirl.build(:trainer)
  @member = FactoryGirl.build(:member)
end

然后在您的哈希中使用它们的ID:

let(:valid_attributes) { {
  'date' => '2020-01-02',
  'starts_at' => '08:00:00',
  'ends_at' => '09:00:00',    
  'member_id' => @member.id,
  'trainer_id' => @trainer.id 
}

在您的代码中,您甚至没有创建它们,只是构建了它们,而仅更改build(:trainer)create(:trainer)在您的情况下不起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法通过角度控制器测试

来自分类Dev

Rspec难以通过构建控制器方法测试#create

来自分类Dev

Rspec测试控制器动作创建逻辑

来自分类Dev

RSpec:在控制器中测试创建操作方法

来自分类Dev

Rspec / Rails控制器测试404无法正常工作

来自分类Dev

RSpec-控制器在模块内部时创建控制器测试

来自分类Dev

Rails控制器Rspec测试

来自分类Dev

Rails控制器rspec失败,规格/控制器出现RoutingError,但通过了个别测试

来自分类Dev

没有通过任何操作时,Rspec控制器测试是否有默认响应?

来自分类Dev

Rspec失败,但通过控制器进行测试时行为有效

来自分类Dev

为什么我的Rspec post:create无效参数控制器测试失败?

来自分类Dev

如何测试控制器帖子:使用 rspec 在 rails 上创建 JSON api?

来自分类Dev

通过服务测试控制器

来自分类Dev

测试对Posts控制器的POST请求

来自分类Dev

如何为无法通过GET访问并使用参数的控制器方法编写RSpec?

来自分类Dev

Rspec控制器测试回调after_save

来自分类Dev

Rails Rspec测试控制器的新动作

来自分类Dev

如何确定rspec控制器测试的主题?

来自分类Dev

使用曲别针进行RSpec控制器测试

来自分类Dev

将标题附加到Rspec控制器测试

来自分类Dev

Rails 4和Rspec-生成控制器测试

来自分类Dev

RSpec控制器测试:没有路由匹配

来自分类Dev

Rspec控制器测试,传递JSON参数

来自分类Dev

使用Devise的Rails 4,使用Rspec测试控制器

来自分类Dev

使用Rails 6上的Rspec测试控制器问题

来自分类Dev

使用Rspec的request-url的测试控制器方法

来自分类Dev

使用rspec 3的测试控制器给出“ @routes is nil”

来自分类Dev

如何告诉Rails卸载控制器(在Rspec测试中)

来自分类Dev

Factory Girl和Rspec控制器测试失败

Related 相关文章

  1. 1

    无法通过角度控制器测试

  2. 2

    Rspec难以通过构建控制器方法测试#create

  3. 3

    Rspec测试控制器动作创建逻辑

  4. 4

    RSpec:在控制器中测试创建操作方法

  5. 5

    Rspec / Rails控制器测试404无法正常工作

  6. 6

    RSpec-控制器在模块内部时创建控制器测试

  7. 7

    Rails控制器Rspec测试

  8. 8

    Rails控制器rspec失败,规格/控制器出现RoutingError,但通过了个别测试

  9. 9

    没有通过任何操作时,Rspec控制器测试是否有默认响应?

  10. 10

    Rspec失败,但通过控制器进行测试时行为有效

  11. 11

    为什么我的Rspec post:create无效参数控制器测试失败?

  12. 12

    如何测试控制器帖子:使用 rspec 在 rails 上创建 JSON api?

  13. 13

    通过服务测试控制器

  14. 14

    测试对Posts控制器的POST请求

  15. 15

    如何为无法通过GET访问并使用参数的控制器方法编写RSpec?

  16. 16

    Rspec控制器测试回调after_save

  17. 17

    Rails Rspec测试控制器的新动作

  18. 18

    如何确定rspec控制器测试的主题?

  19. 19

    使用曲别针进行RSpec控制器测试

  20. 20

    将标题附加到Rspec控制器测试

  21. 21

    Rails 4和Rspec-生成控制器测试

  22. 22

    RSpec控制器测试:没有路由匹配

  23. 23

    Rspec控制器测试,传递JSON参数

  24. 24

    使用Devise的Rails 4,使用Rspec测试控制器

  25. 25

    使用Rails 6上的Rspec测试控制器问题

  26. 26

    使用Rspec的request-url的测试控制器方法

  27. 27

    使用rspec 3的测试控制器给出“ @routes is nil”

  28. 28

    如何告诉Rails卸载控制器(在Rspec测试中)

  29. 29

    Factory Girl和Rspec控制器测试失败

热门标签

归档