使用RSpec创建方法测试失败

时间:2015-10-11 04:56:19

标签: ruby-on-rails rspec

好吧,我一直试图让这个测试工作,并且由于某种原因它不想工作。我不知道我是否错过了什么或什么但我无法弄清楚这个代码到底发生了什么。每件事似乎都指向它是正确的,但我不知道。无论如何这里是我所做的,我认为是正确的,但显然它不是因为它一直在失败。

这些是我的测试属性,无效的属性由于某种原因而失败。

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)>'

错误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)>'

1 个答案:

答案 0 :(得分:0)

当您使用invalid_attributes发送请求时,您的控制器仍然会通过您的代码获取成功路径。你可以从两次失败中看出来。使用<[]>进行渲染会在重定向上发生,如果对象实例已保存,则只有idcreated_at

这表示您未在name模型中验证Survey的存在:

# app/models/survey.rb
class Survey < ActiveRecord::Base
  belongs_to :user

  validates :name, presence: true
end

因此使用空白名称成功保存。