lundi 23 janvier 2017

Receiving 2 errors when running rails test

I have a product model and I'm receiving the following errors when adding validations to the model. I tried to fix the create and update errors by implementing the directions from http://ift.tt/MUotxZ but with no luck. The app was created with rails 5. What do you recommend I should do?

E

Error:
ProductsControllerTest#test_should_update_product:
URI::InvalidURIError: bad URI(is not URI?):http://www.example.com:80update
test/controllers/products_controller_test.rb:38:in `block in <class:ProductsControllerTest>'


bin/rails test test/controllers/products_controller_test.rb:37

E

Error:
ProductsControllerTest#test_should_create_product:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"products"} missing required keys: [:id]
test/controllers/products_controller_test.rb:20:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:19:in `block in <class:ProductsControllerTest>'


bin/rails test test/controllers/products_controller_test.rb:18

..

Finished in 0.446781s, 15.6676 runs/s, 13.4294 assertions/s.

7 runs, 6 assertions, 0 failures, 2 errors, 0 skips

The product.rb :

class Product < ApplicationRecord
   validates :name, :description, :presence => true
   validates :price, numericality: {greater_than_or_equal_to: 0.01}
   validates :name, uniqueness: true
   validates :image, allow_blank: true, format: { with: %r{\.(gif|jpg|png)\Z}i, message: 'must be a URL for GIF, JPG or PNG image.'
}

    mount_uploader :image, ImageUploader
    validates_presence_of :image
end

The products_controller_test.rb :

require 'test_helper'

class ProductsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @product = products(:one)
  end

  test "should get index" do
    get products_url
    assert_response :success
  end

  test "should get new" do
    get new_product_url
    assert_response :success
  end

  test "should create product" do
    assert_difference('Product.count') do
      post product_url, params: { product: { description: 'Rails is awesome!', title: 'Hello Rails' } }
   end

  assert_redirected_to product_path(Product.last)
  assert_equal 'Product was successfully created.', flash[:notice]
  end

  test "should show product" do
    get product_url(@product)
    assert_response :success
  end

  test "should get edit" do
     get edit_product_url(@product)
     assert_response :success
  end

  test "should update product" do
    put :update, id: @product, product: @update
    assert_redirected_to product_url(@product)
  end

  test "should destroy product" do
    assert_difference('Product.count', -1) do
     delete product_url(@product)
   end

    assert_redirected_to products_url
   end
 end

And the products_controller.rb :

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
  end


  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_product
      @product = Product.find(params[:id])
    end


    def product_params
      params.require(:product).permit(:name, :description, :image, :price)
    end
end

Routes.rb :

Rails.application.routes.draw do
  resources :products


  root 'products#index'

end

Aucun commentaire:

Enregistrer un commentaire