我是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
答案 0 :(得分: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