0
我有一些麻煩測試我的rails應用程序。我只是在學習如何測試,所以我認爲這對你來說應該不成問題。 我對這款產品的架構:rails「rake test」錯誤「param is missing」。
create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
這種模式:
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
end
與此控制器:
#some lines omitted...
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
private
def product_params
params.require(:product).permit(:title, :description, :image_url, :price)
end
所以,我乳寧這個測試:
setup do
@product = products(:one)
@update = {
title: 'Lorem Ipsum',
description: 'Wibbles are fun!',
image_url: 'lorem.jpg',
price: 19.95
}
end
test "should create product" do
assert_difference('Product.count') do
process :create, method: :post, params: @update
end
但是我得到0次失敗與1呃ROR。
Error:
ProductsControllerTest#test_should_create_product:
ActionController::ParameterMissing: param is missing or the value is empty: product
app/controllers/products_controller.rb:72:in `product_params'
app/controllers/products_controller.rb:27:in `create'
test/controllers/products_controller_test.rb:30:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:29:in `block in <class:ProductsControllerTest>'
我必須說,我只是升級軌4.2到5.0.0.1。我正在關注「使用rails 5進行敏捷開發」一書,但是如果我像書中所說的那樣做,測試就不起作用,我猜測自從本書寫作以來,rails已經發生了一些變化。 我試圖在很多方面做到這一點,但我無法弄清楚。我究竟做錯了什麼?
感謝您的幫助!