耙子测试失败。为什么我会收到这些错误,如何解决?

时间:2014-08-09 10:45:43

标签: ruby-on-rails ruby validation unit-testing

我正在阅读一些Rails学习资料,其中重点是验证和单元测试。我得到了以下测试错误,可以帮助解决这个问题。有什么想法吗?提前谢谢。

rake test
Run options: --seed 54556

# Running tests:

.F.....F

Finished tests in 0.447052s, 17.8950 tests/s, 33.5531 assertions/s.

  1) Failure:
ProductsControllerTest#test_should_create_product [/depot/test/controllers/products_controller_test.rb:26]:
"Product.count" didn't change by 1.
Expected: 3
  Actual: 2

  2) Failure:
ProductsControllerTest#test_should_update_product [/depot/test/controllers/products_controller_test.rb:45]:
Expected response to be a <redirect>, but was <200>

8 tests, 15 assertions, 2 failures, 0 errors, 0 skips

以下是products_controller_test.rb文件的内容:

1. require 'test_helper'
2.
3. class ProductsControllerTest < ActionController::TestCase
4.   setup do
5.     @product = products (:one)
6.     @update = {
7.       title: 'Lorem Ipsum',
8.       description: 'Wibbles are fun!',
9.       image_url: 'lorem.jpg',
10.       price: 19.95
11.     }
12.  end
...
25. test "should create product" do
26.    assert_difference('Product.count') do
27.      post :create, product: @update
28.    end
...
43. test "should update product" do
44.    put :update, id: @product.to_param, product: @update
45.    assert_redirected_to product_path(assigns(:product))
46. end
...
55. end

这是Controller(products_controller.rb):

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  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 action: 'show', status: :created, location: @product }
      else
        format.html { render action: 'new' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :description, :image_url, :price)
    end
end

这是模型:

class Product < ActiveRecord::Base
    validates :title, :description, :image_url, presence: true
    validates :price, numericality: {greater_than_or_equal_to: 0.01}
    validates :title, uniqueness: true
    validates :image_url, allow_blank: true, format: {
        with: %r{\. (gif|jpg|png)\z}i,
        message: 'must be a URL for GIF, JPG or PNG image.'
    }
end

这是模型测试:

require 'test_helper'

class ProductTest < ActiveSupport::TestCase
  test "product attributes must not be empty" do
    product = Product.new
    assert product.invalid?
    assert product.errors[:title].any?
    assert product.errors[:description].any?
    assert product.errors[:price].any?
    assert product.errors[:image_url].any?
end
end

1 个答案:

答案 0 :(得分:0)

@RockwellRice我设法解决了!在模型中,代替:

  with: %r{\. (gif|jpg|png)\z}i,     

是:

  with: %r{\.(gif|jpg|png)\z}i,

在第一个反斜杠前面的点之后有一个空格,它不应该存在。一旦空间被移除它就有效!在书中看起来空间应该在那里,但它不是。