繁体   English   中英

使用无效参数测试POST时,生成的rspec测试失败

[英]Generated rspec test fails when testing POST with invalid params

我是Rails的新手,并尝试使用无效参数完成为控制器garden生成的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

数据库架构

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

您可以在controller方法内处理异常,因此:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM