rspec中未定义的方法错误

时间:2014-06-29 09:09:41

标签: ruby rspec dsl

我正在实现一个计算器DSL,它已计算为方法中的加,减,乘和除块方法。当我使用'rspec calculator_spec.rb'命令运行rspec测试时,它为'calculate'提供了一个未定义的方法错误。错误如下 -

 1) Calculator Addition operation returns correct values for positive numbers
 Failure/Error: calculate do
 NoMethodError:
   undefined method `calculate' for #<RSpec::ExampleGroups::Calculator::AdditionOperation:0x00000101d14780>
 # ./calculator_spec.rb:16:in `block (3 levels) in <top (required)>'

如何克服此错误?

在calculator.rb中 -

class Calculator
  attr_accessor :total
  def calculate(&block)
    self.total = 0
    instance_eval(&block)
    puts total
  end

  def add(number)
    self.total += number
  end

  def subtract(number)
    self.total -= number
  end

  def multiply(number)
    self.total *= number
  end

  def divide(number)
    self.total /= number
  end
end

在calculator_spec.rb中 -

require './calculator'

describe 'Calculator' do

  describe "Addition operation" do
    it "returns correct values for positive numbers" do
      expect(
          calculate do
            add 2
            add 2
          end
      ).to eq(4)
    end

    it "returns correct values for negative numbers" do
      expect(
          calculate do
            add -2
            add -2
          end
      ).to eq(-4)
    end

    it "returns correct values for positive and negative numbers" do
      expect(
          calculate do
            add 2
            add 10
            add -10
          end
      ).to eq(2)
    end
  end

  describe "Mixed operations" do
    it "returns correct values when mixing all operations" do
      expect(
          calculate do
            add 2
            add -2
            subtract -2
            multiply 10
            divide 10
          end
      ).to eq(2)
    end
  end
end

2 个答案:

答案 0 :(得分:2)

在rspec中更改此行:

describe 'Calculator' do

describe Calculator do

这将创建Calculator类型calculate

除此之外:

  1. nil始终返回def calculate(&block) self.total = 0 instance_eval(&block) total # <- no puts end (这是implicit subject返回的内容):

    calculate
  2. 您需要在subject上致电calculate,最简单的解决方法是在describe区块添加describe Calculator do def calculcate(&block) subject.calculate(&block) end describe "Addition operation" do # ... 方法:

    {{1}}

答案 1 :(得分:1)

我将代码放在名为 test.rb 文件的文件中: -

#!/usr/bin/env ruby

class Calculator 
  attr_accessor :total

  def calculate(&block)
    self.total = 0
    instance_eval(&block)
    total
  end

  def add(number)
    self.total += number
  end

  def subtract(number)
    self.total -= number
  end

  def multiply(number)
    self.total *= number
  end

  def divide(number)
    self.total /= number
  end
end

然后创建了以下 test_spec.rb 文件: -

require_relative "../test.rb"


describe 'Calculator' do

  describe "Addition operation" do

    let(:ob) { Calculator.new }

    it "returns correct values for positive numbers" do
      expect(
        ob.calculate do
          add 2
          add 2                    
        end  
      ).to eq(4)
    end

    it "returns correct values for negative numbers" do
      expect(
        ob.calculate do
          add -2
          add -2
        end  
      ).to eq(-4)
    end

    it "returns correct values for positive and negative numbers" do
      expect(
        ob.calculate do
          add 2
          add 10
          add -10                    
        end  
      ).to eq(2)
    end
  end

  describe "Mixed operations" do
    let(:ob) { Calculator.new }
    it "returns correct values when mixing all operations" do
      expect(
        ob.calculate do
          add 2
          add -2
          subtract -2
          multiply 10
          divide 10                    
        end  
      ).to eq(2)
    end
  end
end

现在运行代码: -

arup@linux-wzza:~/Ruby> rspec -f documentation

Calculator
  Addition operation
    returns correct values for positive numbers
    returns correct values for negative numbers
    returns correct values for positive and negative numbers
  Mixed operations
    returns correct values when mixing all operations

Finished in 0.00197 seconds (files took 0.17971 seconds to load)
4 examples, 0 failures

您的代码无效,因为您在错误的对象上调用方法#calculate。您必须在Calculator课程实例上调用此方法,因为您已定义Calculator#calculate