ruby-on-rails - 使用 Rails 进行敏捷 Web 开发 Book Create Product Test Failure

标签 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

这是我在浏览器中运行的添加产品 Controller 代码:

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

这是我的更新产品 Controller 代码,它再次在浏览器中运行:

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

其中一个测试现在通过了 - 更新一个 - fixtures 中的产品具有相同的标题和标题验证唯一性因此失败。

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

谢谢

最佳答案

您引用的产品夹具在您的产品夹具文件中不存在。 @product = products(:three) 希望在 products.yml 文件中找到名为 :three 的夹具,但您只有 :one 和 :two。创建一个新的产品三固定装置或将 products(:three) 更改为 products(:one)products(:two)

关于ruby-on-rails - 使用 Rails 进行敏捷 Web 开发 Book Create Product Test Failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030492/

相关文章:

android - 如何在真机上运行Robotium测试

ruby-on-rails - 我的 Rails 应用程序如何接受 RAILS_GEM_VERSION 次要版本颠簸

testing - TeSTLink 1.9.4 和 Bugzilla 4.2.3 集成

ruby-on-rails - 未调用 RSpec Controller 测试失败方法

django - model.save() 在加载 Django fixtures 时没有被调用?

java - 在 TestNG 中按顺序执行测试

mysql - 在连接查询中返回不同的记录 - Rails 4

javascript - Rails 渲染 js 文件但无法执行

sql - rails : join query on association with class_name

ruby-on-rails - 如何离线更新rails?