【问题标题】:Ruby RSpec unit test gives undefined method errorRuby RSpec 单元测试给出未定义的方法错误
【发布时间】:2012-08-13 23:11:41
【问题描述】:

我已经使用 RSpec 创建了一个单元测试。

app.rb 有:

module AppModule
  class App
    def get_item
      str = self.get_string
      puts "in get_item - #{str}"
    end

    def get_string
      puts "hello, world"
    end
  end
end

app_test.rb 有:

require 'test_helper'
require 'env'

describe App do
  before :each do
    @var = App.new
  end

  describe "firsttest" do
    it "should print string" do
      @var.get_item
    end
  end
end

我发现 get_item 被正确调用。但是当它到达 get_string 时,我得到一个错误:

#App:0x2eaqc4600 的未定义方法 get_string

谢谢。

【问题讨论】:

  • (无关,但你知道str = get_string,即使它有效,也会将str 设置为nil,对吧?)

标签: ruby unit-testing rspec


【解决方案1】:

看起来像是命名空间问题。我不了解您的环境,但您确定 @var 是 AppModule::App 的实例而不仅仅是 ::App 吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 2014-07-22
    • 1970-01-01
    相关资源
    最近更新 更多