使用Rails Book进行敏捷Web开发会导致产品测试失败

时间:2019-05-07 21:07:54

标签: ruby-on-rails testing

运行测试时出现此错误消息,在浏览器中测试时,相关功能正常工作。

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

现在其中一项测试通过-更新一次-灯具中的产品具有相同的标题,并且标题验证为唯一,从而失败。

任何有关创建产品测试的帮助都会很棒。

谢谢

2 个答案:

答案 0 :(得分:0)

您引用的产品装置文件中没有该产品装置。 @product = products(:three)期望在products.yml文件中找到一个名为:three的灯具,但是您只有:one和:two。要么创建新产品三固定装置,要么将products(:three)更改为products(:one)products(:two)

答案 1 :(得分:0)

设法解决了这个问题,问题是:tite: 'lorxem Ipsum',应该是title: 'lorxem Ipsum',

缺少“ l”!