0

我正在尝试为仓库应用程序编写一个测试用例。下面是代码

模型:

class Product < ActiveRecord::Base
 attr_accessible :description, :image_url, :price, :title

 validates :title, :description, :image_url, :presence => true
 validates :price, :numericality => {:greater_than_or_equal_to => 0.01}

end

测试用例:

require 'spec_helper'

describe Product do
before :each do
    @product = Product.new "Title", "Description", "Image Url", "Price"
end

describe "#new" do
    it "takes four parameters and returns a product" do
        @product.should be_an_instance_of Product
    end
end

describe "#title" do
    it "returns the correct title" do
        @product.title.should eql "Title"
    end
end

describe "#description" do
    it "returns the correct description" do
        @product.description.should eql "Description"
    end
end

describe "#image_url" do
    it "returns the correct image_url" do
        @product.image_url.should eql "Image Url"
    end
end

describe "#price" do
    it "returns the correct price" do
        @product.price.should eql "Price"
    end
end

end

我收到错误

undefined method `has_key?' for nil:NilClass 

ArgumentError:←[0m
1mwrong number of arguments (4 for 2)←[0m
 ./spec/product_spec.rb:5:in `new'←[0m
 ./spec/product_spec.rb:5:in `block (2 levels) \
 in <top (required)>'" for all examples. 

请指导我如何纠正它

4

1 回答 1

1

尝试这样的事情:

before :each do
   @product = Product.new(title: "Title", description: "Description", image_url: "Image Url", price: "Price")
end

试试Michael Hartl 的精彩教程

于 2012-06-29T08:53:02.333 回答