undefined method `before' for main:Object
Asked Answered
I

2

5

I'm getting the above error from section of code below. What is the cause?

 1  require 'spec_helper'
 2
 3  before(:all) do
 4    puts "ServerSpec tests on #{ENV['TARGET_HOST']}"
 5  end

Full error:

/home/newatson/src/serverspec/spec/cfengine3/common_spec.rb:3:in `<top (required)>': undefined method `before' for main:Object (NoMethodError)
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
        from /home/newatson/.gem/ruby/1.9.1/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
        from /home/newatson/.gem/ruby/1.9.1/bin/rspec:19:in `load'
        from /home/newatson/.gem/ruby/1.9.1/bin/rspec:19:in `<main>'
Inexpungible answered 25/5, 2015 at 12:44 Comment(0)
C
8

before should be implemented within describe:

require 'spec_helper'

describe "My Tests" do
  before(:all) do
    puts "ServerSpec tests on #{ENV['TARGET_HOST']}"
  end
end

Good luck!

Chianti answered 25/5, 2015 at 12:46 Comment(0)
I
1

Seems like you missed to describe your class. Without describe Object becomes a receiver and you have a NoMethodError. Easy fix:

describe MyClass do
  before(:all) do
    #
  end
end
Intercom answered 25/5, 2015 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.