2016-04-26 4 views
0

レールで新しく、無効なパラメータでコントローラgardenの生成されたrspecテストを完了しようとしています。無効なパラメータでPOSTをテストすると、生成されたrspecテストが失敗する

常にsquare_feetを入力しますが、エラーは、gardensテーブルの既定値が存在しないと不平を言うようです。これは私を混乱させる。 square_feetのデフォルト値を設定したくないのですが、アプリケーションでエラーが発生せず、ヌル値を検出したときにユーザーをリダイレクトしない理由を理解できません。

私が編集した生成テストの唯一の部分はlet()です。これらのテストに合格するにはどうすればいいですか?

RSPEC試験

RSpec.describe GardensController, type: :controller do 

    let(:valid_attributes) { FactoryGirl.attributes_for(:garden) } 
    let(:invalid_attributes) { {:name => nil, :square_feet => nil, :zone => nil} } 
    let(:valid_session) { {} } 

    describe "POST #create" do 
    context "with invalid params" do 

     it "assigns a newly created but unsaved garden as @garden" do 
     post :create, {:garden => invalid_attributes}, valid_session 
     expect(assigns(:garden)).to be_a_new(Garden) 
     end 

     it "re-renders the 'new' template" do 
     post :create, {:garden => invalid_attributes}, valid_session 
     expect(response).to render_template("new") 
     end  

    end 
    end 

end 

工場

FactoryGirl.define do 
    factory :garden do 
    name "MyString" 
    square_feet 1 
    zone 1 
    garden_type "MyString" 
    user nil 
    end 
end 

DBスキーマ

mysql> describe gardens; 
+-------------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| name  | varchar(255) | NO |  | NULL |    | 
| square_feet | int(11)  | NO |  | NULL |    | 
| zone  | int(11)  | NO |  | NULL |    | 
| garden_type | varchar(255) | YES |  | NULL |    | 
| user_id  | int(11)  | YES | MUL | NULL |    | 
| created_at | datetime  | NO |  | NULL |    | 
| updated_at | datetime  | NO |  | NULL |    | 
+-------------+--------------+------+-----+---------+----------------+ 
8 rows in set (0.00 sec) 

のRSpecのエラー

1) GardensController POST #create with invalid params assigns a newly created but unsaved garden as @garden 
    Failure/Error: if @garden.save 

    ActiveRecord::StatementInvalid: 
     Mysql2::Error: Field 'square_feet' doesn't have a default value: INSERT INTO `gardens` (`created_at`, `updated_at`) VALUES ('2016-04-26 11:25:08', '2016-04-26 11:25:08') 
    # ./app/controllers/gardens_controller.rb:30:in `block in create' 
    # ./app/controllers/gardens_controller.rb:29:in `create' 
    # ./spec/controllers/gardens_controller_spec.rb:91:in `block (4 levels) in <top (required)>' 
    # ------------------ 
    # --- Caused by: --- 
    # Mysql2::Error: 
    # Field 'square_feet' doesn't have a default value 
    # ./app/controllers/gardens_controller.rb:30:in `block in create' 

    2) GardensController POST #create with invalid params re-renders the 'new' template 
    Failure/Error: if @garden.save 

    ActiveRecord::StatementInvalid: 
     Mysql2::Error: Field 'square_feet' doesn't have a default value: INSERT INTO `gardens` (`created_at`, `updated_at`) VALUES ('2016-04-26 11:25:08', '2016-04-26 11:25:08') 
    # ./app/controllers/gardens_controller.rb:30:in `block in create' 
    # ./app/controllers/gardens_controller.rb:29:in `create' 
    # ./spec/controllers/gardens_controller_spec.rb:96:in `block (4 levels) in <top (required)>' 
    # ------------------ 
    # --- Caused by: --- 
    # Mysql2::Error: 
    # Field 'square_feet' doesn't have a default value 
    # ./app/controllers/gardens_controller.rb:30:in `block in create' 

    3) GardensController PUT #update with invalid params assigns the garden as @garden 
    Failure/Error: if @garden.update(garden_params) 

    ActiveRecord::StatementInvalid: 
     Mysql2::Error: Column 'square_feet' cannot be null: UPDATE `gardens` SET `square_feet` = NULL, `zone` = NULL, `updated_at` = '2016-04-26 11:25:08' WHERE `gardens`.`id` = 101 
    # ./app/controllers/gardens_controller.rb:44:in `block in update' 
    # ./app/controllers/gardens_controller.rb:43:in `update' 
    # ./spec/controllers/gardens_controller_spec.rb:131:in `block (4 levels) in <top (required)>' 
    # ------------------ 
    # --- Caused by: --- 
    # Mysql2::Error: 
    # Column 'square_feet' cannot be null 
    # ./app/controllers/gardens_controller.rb:44:in `block in update' 

    4) GardensController PUT #update with invalid params re-renders the 'edit' template 
    Failure/Error: if @garden.update(garden_params) 

    ActiveRecord::StatementInvalid: 
     Mysql2::Error: Column 'square_feet' cannot be null: UPDATE `gardens` SET `square_feet` = NULL, `zone` = NULL, `updated_at` = '2016-04-26 11:25:08' WHERE `gardens`.`id` = 102 
    # ./app/controllers/gardens_controller.rb:44:in `block in update' 
    # ./app/controllers/gardens_controller.rb:43:in `update' 
    # ./spec/controllers/gardens_controller_spec.rb:137:in `block (4 levels) in <top (required)>' 
    # ------------------ 
    # --- Caused by: --- 
    # Mysql2::Error: 
    # Column 'square_feet' cannot be null 
    # ./app/controllers/gardens_controller.rb:44:in `block in update' 

Finished in 0.17718 seconds (files took 3 seconds to load) 
16 examples, 4 failures, 1 pending 

Failed examples: 

rspec ./spec/controllers/gardens_controller_spec.rb:90 # GardensController POST #create with invalid params assigns a newly created but unsaved garden as @garden 
rspec ./spec/controllers/gardens_controller_spec.rb:95 # GardensController POST #create with invalid params re-renders the 'new' template 
rspec ./spec/controllers/gardens_controller_spec.rb:129 # GardensController PUT #update with invalid params assigns the garden as @garden 
rspec ./spec/controllers/gardens_controller_spec.rb:135 # GardensController PUT #update with invalid params re-renders the 'edit' template 
+0

let(:invalid_create) { post :create, {:garden => invalid_attributes}, valid_session } it { expect(invalid_create).to raise_error(ActiveRecord::StatementInvalid) } 

をする場合には、あなたは、コントローラの使用中のトラップ例外にrescue_from定義を望みますここではいくつかのエラーがあります。たとえば、更新テストでスキーマと整列する 'square_feetはnullにできません。 – Anthony

+0

@Anthony:それは間違いですか?私は 'square_feet'もnullでないと信じています。それは ':valid_attributes'にはヌルではなく、':invalid_attributes'にはヌルです(正しいと思われます)。私は ':invalid_attributes'の目的を誤解していますか? ':square_feet => null'のようなものが含まれていなければ、何が入っていますか? – doub1ejack

答えて

2

あなたはとても、コントローラのメソッド内から例外を処理することができます

rescue_from ActiveRecord::StatementInvalid, :with => :render_statement_invalid 
+0

Hm、ok。これらのテストが生成される方法では、私は無効な属性を受け取るとアプリケーションが何らかの形でユーザーを作成ページにリダイレクトすることを知っていると想定していました。しかし、実際には、エラーを検出して対処するロジックを記述するのは実際のところですが、最終的な結果はユーザーが作成ページにリダイレクトされることでしょうか? – doub1ejack

+0

@ doub1ejackが更新されました –

関連する問題