简体   繁体   English

红宝石单元测试问题

[英]ruby Unit Test problem

Have been trying to get the following code to work, but can't; 一直在尝试使以下代码正常工作,但是不能; it says I'm calling a private method. 它说我正在调用私有方法。

What am I doing wrong? 我究竟做错了什么?

    def subtotal(price, qty = 1)
      return nil if price.to_f<= 0 || qty.to_f <= 0
      price.to_f * qty.to_f
    end

    puts subtotal(12.93)
    puts subtotal(12.93, 3)
    puts subtotal(456.82, 6)

     def subtotal(qty = 1)
       return nil if price.to_f<= 0 || qty.to_f <= 0
       self.to_f * qty.to_f
     end

     book = 39.99
     car = 16789

     puts book.subtotal(3)
     puts car.subtotal
     puts car.subtotal(7)

Though I'd like more information, like the full class definition and the error trace, since the public method to_f is the only other method call here, I'm guessing that subtotal is defined as a private method. 尽管我想要更多信息,例如完整的类定义和错误跟踪,但由于公用方法to_f是此处唯一的其他方法调用,因此我猜测subtotal被定义为私有方法。

In a Ruby class definition, keywords like public , protected , and private apply to all methods that come after the keyword. 在Ruby类定义中,诸如publicprotectedprivate类的关键字适用于该关键字之后的所有方法。 Check that the subtotal definition is not below private or protected in the class definition. 检查subtotal定义是否在类定义中不低于privateprotected

If not, then the issue likely lies elsewhere. 如果没有,那么问题可能出在其他地方。 Check that error trace, and look at where the error actually comes from. 检查该错误跟踪,并查看错误的实际来源。

Your subtotal works in two different environments. 您的小计在两个不同的环境中工作。

One time, its a 'stand alone method', the other time it's a Numeric-method. 一次,它是一种“独立方法”,而另一次,它是一种数字方法。 So, lets define them as Numeric-method: 因此,让我们将它们定义为数字方法:

def subtotal(price, qty = 1)
  return nil if price <= 0 || qty <= 0
  price * qty
end

class Numeric
   def subtotal(qty = 1) #only one paramter. The other is 'self'
     return nil if self <= 0 || qty <= 0
     self * qty
   end
 end

puts subtotal(12.93)
puts subtotal(12.93, 3)
puts subtotal(456.82, 6)


 book = 39.99
 car = 16789

 puts book.subtotal(3)
 puts car.subtotal
 puts car.subtotal(7)

Remark on my code: I don't see a real reason for converting to Floats. 在我的代码上注明:我没有看到转换为Floats的真正原因。 If you start with Fixnums (see your car-example), then my solution works also. 如果您从Fixnums开始(请参见您的汽车示例),那么我的解决方案同样适用。 Perhaps, you need the conversion for your specific case. 也许,您需要针对特定​​情况进行转换。

From your example code I would prefer to define a Articcle class: 从您的示例代码中,我希望定义一个Articcle类:

def subtotal(price, qty = 1)
  return nil if price <= 0 || qty <= 0
  price * qty
end

class Article
    def initialize(price = 0)
        @price = price
      end
   def subtotal(qty = 1)
     return nil if @price <= 0 || qty <= 0
     @price * qty
   end
 end

puts subtotal(12.93)
puts subtotal(12.93, 3)
puts subtotal(456.82, 6)


 book = Article.new(39.99)
 car = Article.new(16789)

 puts book.subtotal(3)
 puts car.subtotal
 puts car.subtotal(7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM