Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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 Rails-RSpec命名错误:未定义的方法_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails Rails-RSpec命名错误:未定义的方法

Ruby on rails Rails-RSpec命名错误:未定义的方法,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我试着测试一个非常简单的方法,它接受2个数字,然后用它们计算出一个百分比。但是,当我尝试运行测试时,它失败,出现以下错误: NoMethodError: undefined method `pct' for Scorable:Module ./spec/models/concerns/scorable_spec.rb:328:in `block (2 levels) in <top (required)>' ./spec/rails_helper.rb:97:in `block (

我试着测试一个非常简单的方法,它接受2个数字,然后用它们计算出一个百分比。但是,当我尝试运行测试时,它失败,出现以下错误:

NoMethodError: undefined method `pct' for Scorable:Module
./spec/models/concerns/scorable_spec.rb:328:in `block (2 levels) in 
<top (required)>'
./spec/rails_helper.rb:97:in `block (3 levels) in <top (required)>'
./spec/rails_helper.rb:96:in `block (2 levels) in <top (required)>'
-e:1:in `<main>'
以下是位于Scorable.rb中的pct方法:

def pct(num,den)
  return 0 if num == 0 or num.nil?
  return (100.0 * num / den).round
end
下面是我的rspec_助手:

 if ENV['ENABLE_COVERAGE']
   require 'simplecov'
   SimpleCov.start do
   add_filter "/spec/"
   add_filter "/config/"
   add_filter '/vendor/'

   add_group 'Controllers', 'app/controllers'
   add_group 'Models', 'app/models'
   add_group 'Helpers', 'app/helpers'
   add_group 'Mailers', 'app/mailers'
   add_group 'Views', 'app/views'
 end
end

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
  expectations.include_chain_clauses_in_custom_matcher_descriptions = 
  true
end
config.raise_errors_for_deprecations!

 config.mock_with :rspec do |mocks|
   mocks.verify_partial_doubles = true
 end
end

我是RSpec的新手,一天多来一直对这个问题感到困惑。它肯定指向一个现有的方法,当我在RubyMine中使用Go-to声明时,它会打开方法声明。谁能帮我解释一下这件事吗?我确信我忽略了一些非常简单的事情。

为了使模块方法可以用
模块调用。方法
符号应该在模块范围内声明

module Scorable
  def self.pct(num,den)
    return 0 if num == 0 or num.nil?
    return (100.0 * num / den).round
  end
end
或:

注意,后者在这个模块中声明了模块方法和普通实例方法


旁注:在方法的最后一行使用
return
,被认为是一种代码气味,应避免:

module Scorable
  def self.pct(num,den)
    return 0 if num == 0 or num.nil?
    (100.0 * num / den).round
  end
end

使模块方法可使用
模块调用。应在模块范围内声明方法
符号

module Scorable
  def self.pct(num,den)
    return 0 if num == 0 or num.nil?
    return (100.0 * num / den).round
  end
end
或:

注意,后者在这个模块中声明了模块方法和普通实例方法


旁注:在方法的最后一行使用
return
,被认为是一种代码气味,应避免:

module Scorable
  def self.pct(num,den)
    return 0 if num == 0 or num.nil?
    (100.0 * num / den).round
  end
end

请显示
Scorable
模块的源代码,特别是
pct
方法。Rubymine不是完美的…@mudasobwa用该方法定义更新了问题。请显示
Scorable
模块的源代码,特别是
pct
方法。Rubymine不是完美的…@mudasobwa用该方法定义更新了问题。啊,谢谢!我不敢相信事情竟那么简单。我肯定会记得模函数的事情。模块和实例方法仍然经常在Rails中绊倒我。关于回报声明,这是我正在努力摆脱的一个非常坏的习惯。我来自C#背景,半个月前开始学习Rails,我的直觉仍然经常违背Rails的许多惯例。啊,谢谢!我不敢相信事情竟那么简单。我肯定会记得模函数的事情。模块和实例方法仍然经常在Rails中绊倒我。关于回报声明,这是我正在努力摆脱的一个非常坏的习惯。我来自C#背景,半个月前开始学习Rails,我的直觉仍然经常违背Rails的许多惯例。
module Scorable
  def self.pct(num,den)
    return 0 if num == 0 or num.nil?
    (100.0 * num / den).round
  end
end