Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 运行RSpec测试时的命名错误_Ruby On Rails_Ruby_Ruby On Rails 3_Rspec - Fatal编程技术网

Ruby on rails 运行RSpec测试时的命名错误

Ruby on rails 运行RSpec测试时的命名错误,ruby-on-rails,ruby,ruby-on-rails-3,rspec,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,在尝试使用RSpec测试我创建的gem时,我得到了错误NoMethodError 这是我的珍宝: ~/project/gemz/spec/lib/checksomething.rb 这是我的rspec测试文件: ./project/gemz/spec/lib/checkpercentage_spc.rb require "spec_helper" require "checksomething" RSpec.describe Checkpercentage, '#intNum' do co

在尝试使用RSpec测试我创建的gem时,我得到了错误NoMethodError

这是我的珍宝: ~/project/gemz/spec/lib/checksomething.rb

这是我的rspec测试文件: ./project/gemz/spec/lib/checkpercentage_spc.rb

require "spec_helper"
require "checksomething"

RSpec.describe Checkpercentage, '#intNum' do
  context "with no integer entered" do
    it "must be a number" do
      checkpercentage = Checkpercentage.new
      checkpercentage.findPercent(100, 200)
      expect(checkpercentage.intNum) > 0
    end
  end
end
我想测试findPercentage方法中输入的值是否大于0。但是,当我在终端(rspec spec/lib/checkpercentage_spc.rb)中运行rspec命令时,出现以下错误:

Failures:

1) Checkpercentage#intNum with no integer entered must be a number
   Failure/Error: checkpercentage.findPercent(100, 200)
   NoMethodError: undefined method `findPercent' for #<Checkpercentage:0x9956ca4>
   # ./spec/lib/checkpercentage_spc.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.00089 seconds (files took 0.17697 seconds to load)
1 example, 1 failure

Failed examples:
    rspec ./spec/lib/checkpercentage_spc.rb:6 # Checkpercentage#intNum with no integer entered must be a number
故障:
1) 未输入整数的Checkpercentage#intNum必须是数字
失败/错误:checkpercentage.findPercent(100200)
NoMethodError:未定义的方法“findPercent”#
#./spec/lib/checkpercentage_spc.rb:8:in'block(3级)in'
在0.00089秒内完成(加载文件需要0.17697秒)
1例,1例失败
失败的示例:
rspec./spec/lib/checkpercentage_spc.rb:6#checkpercentage#未输入整数的intNum必须是数字
我是RubyonRails的新手。谁能给我指一下正确的方向吗?感谢您的帮助。

有几件事:

  • 正如Santhosh所写,您声明方法的方式(使用
    self
    )使所有这些方法都适用于
    类本身(
    Checkpercentage
    )。如果希望在
    实例
    Checkpercentage.new
    )上调用它们,则必须从声明中删除
    self
  • 什么是
    intNum
    (看起来像Java,但在Ruby中不存在)?如果我理解正确,您需要检查
    findPercent(amount,base)
    是否返回正数。在这种情况下,is
    expect(checkpercentage.findPercent(100200)).to>0
  • Ruby
    a)
    camelCase
    被避免,取而代之的是
    camel\u case
    和b)方法返回执行的最后一行的结果。这意味着您可以按如下方式重写代码:

    class Checkpercentage
    
      #1) what is  x% of y?
      def find_amount(rate, base)
        rate * base/100
      end 
    
      #2) x is what percentage of y?
      def find_percent(amount, base)
        amount/base * 100
      end
    
      #3) x is y% of what number?
      def find_base(amount, rate)
        amount/rate * 100
      end
    
    end # end Module
    
  • 请注意,我已经删除了
    self
    关键字,以向您展示我的第一点的意思-现在您编写的测试中的语法(
    checkpercentage.method\u name
    )将是正确的


    此外,请注意,您的代码中有一个bug—它可以正常工作,但也不是您想要的。希望您编写的测试能够帮助您找到并修复它,如果不让我们知道的话

    @Santhosh我已经在前一行中通过将类赋给变量“checkpercentage”完成了这项工作:--checkpercentage=checkpercentage.new--checkpercentage.findPercent(100200)类方法只能在类常量上调用。当你创建一个对象时,你定义的方法不可用,因为它是你正在谈论的
    实例
    ,而不是
    定义。是的,通过测试,我可以看到我的代码有一些不正确的地方,但我仍在努力找到正确的地方。不过,我会继续尝试,再次感谢您的帮助:)@VictorAngelo,随时欢迎光临!为了找出错误,试着做一个非常简单的测试,比如
    expect(checkpercentage.find_percent(100200)),等于(0.5)
    。这应该告诉你你期望什么,你得到了什么,你可以从那里开始。
    class Checkpercentage
    
      #1) what is  x% of y?
      def find_amount(rate, base)
        rate * base/100
      end 
    
      #2) x is what percentage of y?
      def find_percent(amount, base)
        amount/base * 100
      end
    
      #3) x is y% of what number?
      def find_base(amount, rate)
        amount/rate * 100
      end
    
    end # end Module