1
好吧,我一直试图使这个测试工作,并由于某种原因,它不想工作。我不知道我是否错过了某些东西或什么,但我无法弄清楚这段代码究竟发生了什么。似乎每件事都指向它是正确的,但我不知道。无论如何,这是我所做的,我认为是正确的,但显然不是因为它一直失败。创建方法测试与RSpec保持一致失败
这些是我的测试属性,无效的属性由于某种原因失败。
let(:valid_attributes){
@user = User.create!(:email => "[email protected]", :password => 'password')
{:name => "name", :user_id => @user.id}
}
let(:invalid_attributes){
@user = User.create!(:email => "[email protected]", :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)>'
错误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)>'
是的,这工作,非常感谢。 – Argus