stevenspiel
stevenspiel

Reputation: 5999

Ruby TDD with Rspec (Basic Questions)

I am trying to run a very basic test with Terminal and Sublime Text 3. My simple test runs, but fails (undefined local variable or method 'x')

My folder hierarchy looks like this:

enter image description here

spec_helper.rb looks like this:

require_relative '../test'

require 'yaml'

test_spec.rb is extremely basic

require 'spec_helper.rb'

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

and my test.rb file has x = 5 That's it.

Will a variable only be recognizable if it's part of a class? And do I need to call a new class every time I run my test?

Upvotes: 0

Views: 63

Answers (1)

zetetic
zetetic

Reputation: 47548

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.

Upvotes: 1

Related Questions