0

我正在尝试使用 Terminal 和 Sublime Text 3 运行一个非常基本的测试。我的简单测试运行,但失败(undefined local variable or method 'x'

我的文件夹层次结构如下所示:

在此处输入图像描述

spec_helper.rb 看起来像这样:

require_relative '../test'

require 'yaml'

test_spec.rb 非常基础

require 'spec_helper.rb'

describe "testing ruby play" do
  it "finds if x is equal to 5" do
    x.should eql 5
  end
end

我的 test.rb 文件x = 5就是这样。

一个变量只有在它是一个类的一部分时才能被识别吗?每次运行测试时都需要调用一个新类吗?

4

1 回答 1

1

From the docs

require(name) → true or false

Loads the given name, returning true if successful and false if the feature is already loaded.

[snip]

Any constants or globals within the loaded source file will be available in the calling program’s global namespace. However, local variables will not be propagated to the loading environment.

You could use a constant in your required file:

X = 5
...
X.should eql 5 # => passes

But you probably want to do something entirely different here. Perhaps you could expand on the question and explain what you are trying to accomplish.

于 2013-11-15T02:03:35.013 回答