Forum: Ruby Help request: failing to create an instance of a class within a spec

8a09bac02c91b120d34a6c7635ad45ee?d=identicon&s=25 Sjouke Fikse (Guest)
on 2014-07-22 02:36
(Received via mailing list)
Hi everyone, I'm doing my best to get up to speed in ruby and rails.
Within
a test project, I'm trying to create an instance of a class in a spec
file,
but I'm failing to do this.

These two files exists:
  app/models/street.rb
  spec/models/street_spec.rb

Within the spec, I'm trying to create a new instance of the street
class,
but it doesn't know what street is. That's acceptable, I add `require
'streets'` and it still fails with

$ rspec spec/models/street_spec.rb
/home/sjouke/streetmap/spec/models/street_spec.rb:2:in `require': cannot
load such file -- street (LoadError)
  from /home/sjouke/streetmap/spec/models/street_spec.rb:2:in `<top
(required)>'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
`load'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
`block in load_spec_files'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
`each'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in
`load_spec_files'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:97:in
`setup'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:85:in
`run'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:in
`run'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:in
`invoke'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/gems/rspec-core-3.0.2/exe/rspec:4:in
`<top (required)>'
  from /home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/bin/rspec:23:in
`load'
  from /home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/bin/rspec:23:in
`<main>'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/bin/ruby_executable_hooks:15:in
`eval'
  from
/home/sjouke/.rvm/gems/ruby-2.1.2@streetmap/bin/ruby_executable_hooks:15:in
`<main>'

Any idea why it wouldn't be loading the file appropriately?
I'm also using rspec-rails.
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2014-07-22 04:39
(Received via mailing list)
On Jul 21, 2014, at 17:35, Sjouke Fikse <sjouke.fikse@gmail.com> wrote:

> Hi everyone, I'm doing my best to get up to speed in ruby and rails. Within a
test project, I'm trying to create an instance of a class in a spec file, but 
I'm
failing to do this.

This is most likely a rails question (or rspec) and is better addressed
to one of their lists/forums.
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2014-07-22 08:32
(Received via mailing list)
Excerpts from Sjouke Fikse's message of 2014-07-22 02:35:45 +0200:
> 'streets'` and it still fails with
>
> $ rspec spec/models/street_spec.rb
> /home/sjouke/streetmap/spec/models/street_spec.rb:2:in `require': cannot
> load such file -- street (LoadError)
...
> Any idea why it wouldn't be loading the file appropriately?
> I'm also using rspec-rails.

I think the issue is that you need to tell require where 'street.rb'
really is.
I never used rails, so I can't say if rspec is supposed to be able to
determine
it automatically, but otherwise you'd need to either write

require './app/models/street.rb'

or

require_relative '../../../app/models/street.rb'

or call rspec adding your application directory to the load path, using
the -I
option: rspec -I app

and use

require 'models/street'

I hope this helps

Stefano
8a09bac02c91b120d34a6c7635ad45ee?d=identicon&s=25 Sjouke Fikse (Guest)
on 2014-07-22 17:16
(Received via mailing list)
Oh, I'm sorry, I did post this in the Ruby on Rails forum as well. I
honestly had forgotten where I posted it, and it looks like I cross
posted.
I am sorry, thanks for letting me know about the list. I will continue
the
conversation in there (after responding to Stefano).


On Mon, Jul 21, 2014 at 9:38 PM, Ryan Davis <ryand-ruby@zenspider.com>
8a09bac02c91b120d34a6c7635ad45ee?d=identicon&s=25 Sjouke Fikse (Guest)
on 2014-07-22 17:28
(Received via mailing list)
Strangely, everything works, I don't know why. I didn't even need to use
the -I flag or the require statement. I wonder if was because I started
using `bundle exec rspec`  instead of `rspec`.


On Tue, Jul 22, 2014 at 1:40 AM, Stefano Crocco
<stefano.crocco@alice.it>
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.