Reputation: 431
I'm having some troubles testing my rails app. I'm just learning how to test, so I think this should be no problem for you. I have this product schema:
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
this model:
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
and this controller:
#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
So, I'm runing this test:
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
But I get 0 failures with 1 error.
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>'
I must say that i'm just upgrade rails 4.2 to 5.0.0.1. and I'm following the "agile development with rails 5" book, but the test doesn't work if I do it like the book says, I guess rails has changed in someway since the book was write. I have tried to do this in many ways but I could not figure it out. What am I doing wrong?
Thanks for any help!
Upvotes: 0
Views: 59
Reputation: 14900
My guess is that the @update
has to look like this
@update = {
product: {
title: 'Lorem Ipsum',
description: 'Wibbles are fun!',
image_url: 'lorem.jpg',
price: 19.95
}
}
Upvotes: 2