簡體   English   中英

使用Rails Book進行敏捷Web開發會導致產品測試失敗

[英]Agile Web Development with Rails Book Create Product Test Failure

運行測試時出現此錯誤消息,在瀏覽器中測試時,相關功能正常工作。

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

這是我仔細檢查過的測試。

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

結束

這是我的固定裝置

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

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

這是我在瀏覽器中可用的添加產品控制器代碼:

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

任何幫助或指導表示贊賞。

編輯:

這是測試日志:

ProductsControllerTest: test_should_create_product
--------------------------------------------------
  Product Load (0.2ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT ?  [["id", 980190962], ["LIMIT", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "products"
Started POST "/products" for 127.0.0.1 at 2019-05-08 08:35:35 +0100
Processing by ProductsController#create as HTML
  Parameters: {"product"=>{"tite"=>"lorxem Ipsum", "description"=>"Wibbles are fun!", "image_url"=>"lorem.jpg", "price"=>"19.95"}}
Unpermitted parameter: :tite
   (0.1ms)  SAVEPOINT active_record_1
  Product Exists (0.3ms)  SELECT  1 AS one FROM "products" WHERE "products"."title" IS NULL LIMIT ?  [["LIMIT", 1]]
   (0.1ms)  ROLLBACK TO SAVEPOINT active_record_1
  Rendering products/new.html.erb within layouts/application
  Rendered products/_form.html.erb (4.3ms)
  Rendered products/new.html.erb within layouts/application (4.9ms)
Completed 200 OK in 13ms (Views: 7.1ms | ActiveRecord: 0.5ms)
   (0.1ms)  SELECT COUNT(*) FROM "products"
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction

現在,一項測試通過-更新一項-固定裝置中的產品具有相同的標題,並且標題驗證為唯一,從而失敗。

創建產品測試的任何幫助都將非常有用。

謝謝

您引用的產品夾具文件中沒有產品夾具。 @product = products(:three)期望在products.yml文件中找到一個名為:three的燈具,但是您只有:one和:two。 要么創建一個新產品三個夾具,要么將更改products(:three)更改為products(:one)products(:two)

設法工作了這一點,這個問題是: tite: 'lorxem Ipsum',應該是title: 'lorxem Ipsum',

缺少的“ l”!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM