Rspec没有找到类方法(Rspec not finding class methods)

编程入门 行业动态 更新时间:2024-10-26 18:24:30
Rspec没有找到类方法(Rspec not finding class methods)

我正在为我的后端工作编写一些测试,我有一个奇怪的问题,rspec没有找到我的方法。

我写了一个简单的类和测试来说明问题:

app / interactors / tmp_test.rb:

class TmpTest def call a = 10 b = 5 b.substract_two return a + b end def substract_two c = self - 2 return c end end

spec / interactors / tmp_test.rb:

require 'rails_helper' describe TmpTest do context 'when doing the substraction' do it 'return the correct number' do expect(described_class.call).to eq(13) end end end

输出:

TmpTest when doing the substraction return the correct number (FAILED - 1) Failures: 1) TmpTest when doing the substraction return the correct number Failure/Error: expect(described_class.call).to eq(13) NoMethodError: undefined method `call' for TmpTest:Class # ./spec/interactors/tmp_test.rb:6:in `block (3 levels) in <top (required)>' Finished in 0.00177 seconds (files took 1.93 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/interactors/tmp_test.rb:5 # TmpTest when doing the substraction return the correct number

I'm writing some tests for my backend jobs and I'm having a weird issue with rspec not finding my methods.

I wrote a simple class & test to illustrate the issue :

app/interactors/tmp_test.rb :

class TmpTest def call a = 10 b = 5 b.substract_two return a + b end def substract_two c = self - 2 return c end end

spec/interactors/tmp_test.rb :

require 'rails_helper' describe TmpTest do context 'when doing the substraction' do it 'return the correct number' do expect(described_class.call).to eq(13) end end end

output:

TmpTest when doing the substraction return the correct number (FAILED - 1) Failures: 1) TmpTest when doing the substraction return the correct number Failure/Error: expect(described_class.call).to eq(13) NoMethodError: undefined method `call' for TmpTest:Class # ./spec/interactors/tmp_test.rb:6:in `block (3 levels) in <top (required)>' Finished in 0.00177 seconds (files took 1.93 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/interactors/tmp_test.rb:5 # TmpTest when doing the substraction return the correct number

最满意答案

它不是类方法,而是实例方法。 您的测试应如下所示:

describe TmpTest do subject(:instance) { described_class.new } context 'when doing the subtraction' do it 'returns the correct number' do expect(instance.call).to eq(13) end end end

It's not a class method, it's an instance method. Your test should look like this:

describe TmpTest do subject(:instance) { described_class.new } context 'when doing the subtraction' do it 'returns the correct number' do expect(instance.call).to eq(13) end end end

更多推荐

本文发布于:2023-08-07 22:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有找到   方法   Rspec   methods   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!