Ruby on rails 使用RSpec创建方法测试持续失败
好吧,我一直在努力让这个测试工作,但出于某种原因,它不想工作。我不知道我是否遗漏了什么,但我不知道这段代码到底是怎么回事。似乎每件事都表明它是正确的,但我不知道。无论如何,这就是我所做的,我认为是正确的,但显然不是,因为它一直失败 这些是我的测试属性,无效属性由于某种原因失败Ruby on rails 使用RSpec创建方法测试持续失败,ruby-on-rails,rspec,Ruby On Rails,Rspec,好吧,我一直在努力让这个测试工作,但出于某种原因,它不想工作。我不知道我是否遗漏了什么,但我不知道这段代码到底是怎么回事。似乎每件事都表明它是正确的,但我不知道。无论如何,这就是我所做的,我认为是正确的,但显然不是,因为它一直失败 这些是我的测试属性,无效属性由于某种原因失败 let(:valid_attributes){ @user = User.create!(:email => "email@gmail.com", :password => 'password'
let(:valid_attributes){
@user = User.create!(:email => "email@gmail.com", :password => 'password')
{:name => "name", :user_id => @user.id}
}
let(:invalid_attributes){
@user = User.create!(:email => "email@gmail.com", :password => 'password')
{:name => "", :user_id => @user.id}
}
以下是我的发帖请求:
describe "POST #create" do
context "with valid attributes" do
it "describes a survey created with valid attributes" do
expect{
post :create, survey: valid_attributes
}.to change(Survey, :count).by(1)
end
it "redirects the user to the survey's detail page" do
post :create, {survey: valid_attributes}
expect(response).to redirect_to(Survey.last)
end
end
context "with invalid attributes" do
it "describes a survey created with invalid attributes" do
post :create, {survey: invalid_attributes}
expect(assigns(:survey)).to be_a_new(Survey)
end
it "re-renders the new template" do
post :create, {survey: invalid_attributes}
expect(response).to render_template('new')
end
end
end
当然,我的控制器方法已经实现了,所以这不应该失败,特别是因为它正在做那些东西所表明的事情
def create
@survey = Survey.new(survey_params)
respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
else
format.html { render :new }
end
end
end
我也在使用强参数,不知道这是否会产生影响,我认为不应该,但不管怎样,这就是他们所拥有的:
def set_survey
@survey = Survey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
params.require(:survey).permit(:name, :user_id)
end
最后,这是我的错误消息所说的,这对我来说毫无意义,尤其是第一条,因为对象似乎符合作为调查的所有标准
错误1:
1) SurveysController POST #create with invalid attributes describes a survey created with invalid attributes
Failure/Error: expect(assigns(:survey)).to be_a_new(Survey)
expected #<Survey id: 187, name: "", user_id: 257, created_at: "2015-10-11 04:46:35", updated_at: "2015-10-11 04:46:35"> to be a new Survey(id: integer, name: string, user_id: integer, created_at: datetime, updated_at: datetime)
# ./spec/controllers/surveys_controller_spec.rb:82:in `block (4 levels) in <top (required)>'
1)SurveysController POST#使用无效属性创建描述使用无效属性创建的调查
失败/错误:期望(分配(:调查))。成为新的(调查)
应为新调查(id:integer,名称:string,用户id:integer,创建时间:datetime,更新时间:datetime)
#./spec/controllers/surveys\u controller\u spec.rb:82:in'block(4层)in'
错误2:
2) SurveysController POST #create with invalid attributes re-renders the new template
Failure/Error: expect(response).to render_template('new')
expecting <"new"> but rendering with <[]>
# ./spec/controllers/surveys_controller_spec.rb:87:in `block (4 levels) in <top (required)>'
2)SurveysController POST#使用无效属性创建重新呈现新模板
失败/错误:预期(响应)。要呈现_模板(“新建”)
预期但渲染为
#./spec/controllers/surveys\u controller\u spec.rb:87:in'block(4层)in'
当您使用无效的\u属性发送请求时,您的控制器仍然会通过您的代码执行成功路径。你可以从你的两次失败中辨别出来。在重定向时使用
进行渲染,并且对象实例只有id
和已创建(如果已保存)
这表明您没有验证您的调查
模型中是否存在名称
:
# app/models/survey.rb
class Survey < ActiveRecord::Base
belongs_to :user
validates :name, presence: true
end
#app/models/survey.rb
班级调查
因此,它会以一个空白名称成功保存。发生的情况是,当您使用无效属性发送请求时,您的控制器仍然会通过您的代码执行成功路径。你可以从你的两次失败中辨别出来。在重定向时使用
进行渲染,并且对象实例只有id
和已创建(如果已保存)
这表明您没有验证您的调查
模型中是否存在名称
:
# app/models/survey.rb
class Survey < ActiveRecord::Base
belongs_to :user
validates :name, presence: true
end
#app/models/survey.rb
班级调查
因此,它被成功地保存为空名称