rails lib/* path in rspecs

27 views
Skip to first unread message

Igor Yurchenko

unread,
Sep 26, 2015, 10:12:49 AM9/26/15
to rspec
Hi, ppl...

I need help with setting up rails evironment for rspec. I've created some code on lib/backup/status.rb path and matched specs at spec/lib/backup/status_spec.rb.

The problem is the spec does not see Backup::Status constant defined at lib/backup/status.rb...


Row "RSpec.describe Backup::Status do end" in spec/lib/backup/status.rb causes error:
/home/space/devel/sole/sola/spec/lib/backup/status_spec.rb:4:in `<top (required)>': uninitialized constant Backup (NameError)

Thanks in advance...

Myron Marston

unread,
Sep 26, 2015, 10:14:31 AM9/26/15
to rs...@googlegroups.com
RSpec does not do any constant autoloading so if you're getting and uninitialized constant error but the constant is defined in a file, it means that file hasn't been loaded yet.  The simple solution is to `require` the file.

HTH,
Myron

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/49a5a495-cc57-4906-b69f-b3589319cc7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Igor Yurchenko

unread,
Sep 26, 2015, 1:56:02 PM9/26/15
to rspec
Thank you for answer. The adding "require 'backup/status'" causes error in lib/backup/status.rb:

20:14:54 - INFO - Running: spec/lib/backup/status_spec.rb
Coverage report generated for RSpec to /home/space/devel/sole/sola/coverage. 1 / 7 LOC (14.29%) covered.
/home/space/devel/sole/sola/lib/backup/status.rb:1:in `<top (required)>': uninitialized constant Backup (NameError)
        from /home/space/devel/sole/sola/spec/lib/backup/status_spec.rb:2:in `require'
        from /home/space/devel/sole/sola/spec/lib/backup/status_spec.rb:2:in `<top (required)>'

lib/backup/status.rb contains:

class Backup::Status
...
end

I have supposed gem rspec_rails is responsible for similar kind of problem. But...

I would be very gratefull for any ideas how to solve this problem...


суббота, 26 сентября 2015 г., 17:14:31 UTC+3 пользователь Myron Marston написал:

Myron Marston

unread,
Sep 26, 2015, 5:47:06 PM9/26/15
to rs...@googlegroups.com

In Ruby, if you have a class Foo::Bar definition, it will fail with an error like the one you received if Foo has not already been defined. That’s simply how Ruby works and has nothing to do with RSpec. The solution is to change your class definition to:

module Backup
  class Status
    # ...
  end
end

(Note: that could be class Backup if that’s what you intend it to be.)

Alternately, if you want to keep the class Backup::Status form, you’ll have to require another file that does define the bare Backup class or module. Typically this require would go at the top of your lib/backup/status.rb file.

HTH,
Myron


Igor Yurchenko

unread,
Sep 26, 2015, 7:52:51 PM9/26/15
to rspec
Oh, my god... I feel myself stupid... I should guess this solution myself...

Thanks alot Myron...

воскресенье, 27 сентября 2015 г., 0:47:06 UTC+3 пользователь Myron Marston написал:
Reply all
Reply to author
Forward
0 new messages