我一直试图解决这个问题一段时间,但没有运气。我将通过Agile rails developmnet书,当我运行rake测试时,我在Iteration B1得到2个错误验证:
Error:
ProductsControllerTest#test_should_create_product:
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80create
test/controllers/products_controller_test.rb:26:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:25:in `block in <class:ProductsControllerTest>'
Error:
ProductsControllerTest#test_should_update_product:
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80update
test/controllers/products_controller_test.rb:43:in `block in <class:ProductsControllerTest>'
有人可以帮我解决这个问题!
产品与GT; controller_test.rb
require 'test_helper'
class ProductsControllerTest < ActionDispatch::IntegrationTest
setup do
@product = products(:one)
@update = {
title: 'Lorem 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 :create, 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
put :update, id: @product, 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
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 :show, status: :created, location: @product }
else
format.html { render :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 { render :show, status: :ok, location: @product }
else
format.html { render :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, notice: 'Product was successfully destroyed.' }
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
products.rb
class Product < ApplicationRecord
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