【发布时间】:2017-12-09 03:43:36
【问题描述】:
我正在尝试测试一种非常简单的方法,该方法接受 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>'
这是我的模块规范文件:
require 'rails_helper'
RSpec.describe Scorable, :type => :concern do
it "pct should return 0 if den is 0 or nil" do
expect(Scorable.pct(nil, 15)).to eq(0)
expect(Scorable.pct(0, 15)).to eq(0)
end
end
这是位于 Scorable.rb 中的 pct 方法:
def pct(num,den)
return 0 if num == 0 or num.nil?
return (100.0 * num / den).round
end
这是我的 rspec_helper:
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 Declaration 时,它会打开方法声明。任何人都可以为我解释一下这个吗?我确定我忽略了一些非常简单的事情。
【问题讨论】:
-
请出示
Scorable模块的源码,具体是pct方法。 -
Rubymine 并不完美...
-
@mudasobwa 已使用该方法定义更新了问题。
标签: ruby-on-rails ruby rspec