[英]Rake Test Failure. Why am I getting these errors and how do I fix them?
我正在閱讀一些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
這是控制器(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
@RockwellRice我設法解決了! 在模型中, 代替:
with: %r{\. (gif|jpg|png)\z}i,
它是:
with: %r{\.(gif|jpg|png)\z}i,
第一個反斜杠前面的點后有一個空格,不應存在。 一旦空間被刪除,它就可以工作! 在書中,看起來應該有該空間,但實際上沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.