我是新的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
這裏有一些錯誤,比如你的更新測試說'square_feet不能爲null',它與你的模式一致。 – Anthony
@Anthony:這是一個錯誤嗎?我也相信'square_feet'不應該爲null。它在':valid_attributes'中不爲null,在':invalid_attributes'中它爲null(看起來正確)。我誤解了':invalid_attributes'的用途嗎?如果它不應該包含像':square_feet => null'這樣的東西,它應該包含什麼? – doub1ejack