2017-04-12 7 views
0

私のレールアプリをテストするのにいくつかの問題があります。私はちょうどテストする方法を学んでいるので、これはあなたのためには問題ではないと思う。 私はこの製品のスキーマを持っている: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 

と、このコントローラ:

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 

#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 

だから、私はこのテストをruningています

しかし、私は1つのエラーで0を得る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のアップグレードレールです。私は「レール5のアジャイル開発」の本を守っていますが、この本のようにテストしても機能しない場合は、本が書かれてからレールが幾分変わったと思います。 私はこれを多くの方法でやろうとしましたが、私はそれを理解できませんでした。私は間違って何をしていますか?

ありがとうございました!

答えて

2

私の推測では、@updateこの

@update = { 
    product: { 
    title: 'Lorem Ipsum', 
    description: 'Wibbles are fun!', 
    image_url: 'lorem.jpg', 
    price: 19.95 
    } 
} 
見えるようにならないことです
関連する問題