簡體   English   中英

創建方法測試因RSpec不斷失敗

[英]Create method test keeps failing with 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)>'

發生的是,當您使用invalid_attributes發送請求時,控制器仍然采用成功的代碼路徑。 您可以從兩次失敗中看出來。 使用<[]>渲染發生在重定向上,並且對象實例只有保存后才具有idcreated_at

這表明您沒有在Survey模型中驗證name存在:

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

  validates :name, presence: true
end

因此,成功保存了一個空白名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM