Rspec POST 导致未定义的方法“key?”对于:创建:符号
Rspec POST results in undefined method `key?' for :create:Symbol
在测试我的控制器时,我在 Rspec 中有一个规范,使用 factorygirl 作为数据;
describe 'POST #create' do
context 'with valid attributes' do
it 'saves the new child in the database' do
expect{
post '/children', :create, person: attributes_for(:child)
}.to change(Person, :count).by(1)
end
我收到一个错误
1) PeopleController POST #create with valid attributes saves the new child in the database
Failure/Error: post '/children', :create, person: attributes_for(:child)
NoMethodError:
undefined method key?' for :create:Symbol
# ./spec/controllers/people_controller_spec.rb:46:in
block (5 levels) in '
# ./spec/controllers/people_controller_spec.rb:45:in `block (4 levels) in '
我的控制器(这是无关紧要的一切,非常抱歉)
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
before_action :set_type
def full_name
[Person.christian_name, Person.surname].join ' '
end
def index
@people = type_class.all
end
def new
@person = type_class.new
end
def update
end
def show
end
def edit
end
def create
if @person_type == 'Child'
@person = Child.new(person_params)
else
@person = CareGiver.new(person_params)
end
if @person.save
redirect_to @person
else
render 'new'
end
end
def destroy
end
private
def person_params
if @person_type == 'Child'
params.require(:child).permit(:type, :christian_name, :surname, :dob, :gender)
else
params.require(:care_giver).permit(:type, :christian_name, :surname, :email,
:password, :password_confirmation)
end
end
def set_type
@person_type = type
end
def type
Person.types.include?(params[:type]) ? params[:type] : "Person"
end
def people_types
%w[Person Child Provider CareGiver]
end
def type_class
type.constantize if type.in? people_types
end
def set_person
@person = type_class.find(params[:id])
end
end
这是将不同类型的人保存到一个 table (sti)。
似乎没有参数传递给控制器。当我此时使用调试器时,我可以创建一个 Child 对象,但 post 不会传递它。我添加了 '/children' 以获得正确的路线(尽管我读到 rspec 控制器测试绕过路线堆栈)
我盯着它看了好几个小时,我的胃里有一种你不知道为什么的感觉!
感谢您的帮助
def set_type
@person_type = type
end
def type
Person.types.include?(params[:type]) ? params[:type] : "Person"
end
在我的控制器中找到此代码。 type 方法没有设置@person_type 可以使用的值。我摆脱了方法类型,并将 set_type 更改为
def set_type
@person_type = params[:person][:type]
end
不确定这是否安全?但现在测试通过了。
p.s。所有其他测试都失败了!!
在测试我的控制器时,我在 Rspec 中有一个规范,使用 factorygirl 作为数据;
describe 'POST #create' do
context 'with valid attributes' do
it 'saves the new child in the database' do
expect{
post '/children', :create, person: attributes_for(:child)
}.to change(Person, :count).by(1)
end
我收到一个错误
1) PeopleController POST #create with valid attributes saves the new child in the database Failure/Error: post '/children', :create, person: attributes_for(:child) NoMethodError: undefined method
key?' for :create:Symbol # ./spec/controllers/people_controller_spec.rb:46:in
block (5 levels) in ' # ./spec/controllers/people_controller_spec.rb:45:in `block (4 levels) in '
我的控制器(这是无关紧要的一切,非常抱歉)
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
before_action :set_type
def full_name
[Person.christian_name, Person.surname].join ' '
end
def index
@people = type_class.all
end
def new
@person = type_class.new
end
def update
end
def show
end
def edit
end
def create
if @person_type == 'Child'
@person = Child.new(person_params)
else
@person = CareGiver.new(person_params)
end
if @person.save
redirect_to @person
else
render 'new'
end
end
def destroy
end
private
def person_params
if @person_type == 'Child'
params.require(:child).permit(:type, :christian_name, :surname, :dob, :gender)
else
params.require(:care_giver).permit(:type, :christian_name, :surname, :email,
:password, :password_confirmation)
end
end
def set_type
@person_type = type
end
def type
Person.types.include?(params[:type]) ? params[:type] : "Person"
end
def people_types
%w[Person Child Provider CareGiver]
end
def type_class
type.constantize if type.in? people_types
end
def set_person
@person = type_class.find(params[:id])
end
end
这是将不同类型的人保存到一个 table (sti)。 似乎没有参数传递给控制器。当我此时使用调试器时,我可以创建一个 Child 对象,但 post 不会传递它。我添加了 '/children' 以获得正确的路线(尽管我读到 rspec 控制器测试绕过路线堆栈) 我盯着它看了好几个小时,我的胃里有一种你不知道为什么的感觉! 感谢您的帮助
def set_type
@person_type = type
end
def type
Person.types.include?(params[:type]) ? params[:type] : "Person"
end
在我的控制器中找到此代码。 type 方法没有设置@person_type 可以使用的值。我摆脱了方法类型,并将 set_type 更改为
def set_type
@person_type = params[:person][:type]
end
不确定这是否安全?但现在测试通过了。 p.s。所有其他测试都失败了!!