mardi 7 mai 2019

Agile Web Development with Rails Book Create Product Test Failure

I am getting this error message when I run my tests, the relevant features work when tested in the browser.

Error:
ProductsControllerTest#test_should_create_product:
StandardError: No fixture named 'three' found for fixture set 
'products'
    test/controllers/products_controller_test.rb:5:in `block in 
<class:ProductsControllerTest>'


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

Error:
ProductsControllerTest#test_should_get_index:
StandardError: No fixture named 'three' found for fixture set 
'products'
test/controllers/products_controller_test.rb:5:in `block in 
<class:ProductsControllerTest>'


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

Here are my tests which i have checked carefully.

require 'test_helper'

class ProductsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @product = products(:three)
    @update = {
        tite:         'lorxem Ipsum',
        description:  'Wibbles are fun!',
        image_url:    'lorem.jpg',
        price:        19.95
    }
  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 products_url, params: { product: @update }
  end

  assert_redirected_to product_url(Product.last)
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
    patch product_url(@product), params: { 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 here are my fixtures

one:
  title: MyString
  description: MyText
  image_url: test.jpg
  price: 9.99

two:
  title: MyString
  description: MyText
  image_url: test.jpg
  price: 9.99

Here is my add product controller code which works in the broweser:

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

And here is my update product controller code which again works in the browser:

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

Any help or guidance appreciated.

Aucun commentaire:

Enregistrer un commentaire