2016-04-26 77 views
0

我是新的rails并试图为我的控制器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

这里有一些错误,比如你的更新测试说'square_feet不能为null',它与你的模式一致。 – Anthony

+0

@Anthony:这是一个错误吗?我也相信'square_feet'不应该为null。它在':valid_attributes'中不为null,在':invalid_attributes'中它为null(看起来正确)。我误解了':invalid_attributes'的用途吗?如果它不应该包含像':square_feet => null'这样的东西,它应该包含什么? – doub1ejack

回答

2

您可以从控制器方法内处理异常,所以:如果

let(:invalid_create) { post :create, {:garden => invalid_attributes}, valid_session } 

it { expect(invalid_create).to raise_error(ActiveRecord::StatementInvalid) } 

你想陷阱异常的控制器使用rescue_from定义:

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

嗯,好的。这些测试生成的方式,我假设在接收到无效属性时,应用程序会以某种方式知道将用户重定向到创建页面。但是,实际上,编写逻辑来检测和解决错误实际上取决于我,最终的结果是用户被重定向到创建页面? – doub1ejack

+0

@ doub1ejack已更新 –