ray测试中的ruby:模型语法错误

时间:2014-09-22 09:54:46

标签: ruby-on-rails ruby

您好我已在applicationname/test/models/product_test.rb

下更新了我的文件以进行测试

进行这些测试

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

 test "product price must be positive" do
    product = Product.new(title: "My Book Title", description: "yyy", image_url: "zzz.jpg") 

    product.price = -1
    assert product.invalid?
    assert_equal["must be greater than or equal to 0.01"],
    product.errors[:price]

    product.price = 0 
    assert product.invalid?
    assert_equal["must be greater than or equal to 0.01"],
    product.errors[:price]

    product.price = 1
    assert product.valid?

 end

end

但是当我做rake test:models

我收到错误

SyntaxError: C:/work/applicationname/test/models/product_test.rb:19: syntax error, unexpected '\n' expexting : "="
SyntaxError: C:/work/applicationname/test/models/product_test.rb:24: syntax error, unexpected '\n' expexting : "="

1 个答案:

答案 0 :(得分:0)

而不是:

assert_equal["must be greater than or equal to 0.01"],
product.errors[:price]

你应该:

assert_equal ["must be greater than or equal to 0.01"], product.errors[:price]

代码中出现两次,因此请务必修复两个外观。