Reputation: 9009
I'm trying to use rspec3 for the first time with a new app. I've defined two simple models:
class Match < ActiveRecord::Base
end
class Article < ActiveRecord::Base
has_many :matches
end
When I access these models from the console, I can create the associations and store them in the DB as expected. But, when I try to run a spec to test this, it doesn't seem to work:
require 'spec_helper'
describe Article do
it { is_expected.to have_many(:matches) }
# it { should have_many(:matches) }
end
I tried running both the isexpected
version and the should
version of the test, but in both cases, I get the following error:
> ./bin/rspec spec/models/article_spec.rb
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
F
Failures:
1) Article should have many :matches
Failure/Error: it { is_expected.to have_many :matches }
expected #<Article:0x007fa14c276f38> to respond to `has_many?`
# ./spec/models/article_spec.rb:4:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
Finished in 0.09484 seconds (files took 0.63853 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/article_spec.rb:4 # Article should have many :matches
Randomized with seed 24636
Here are the relevant dependencies in Gemfile.lock:
rspec-core (3.0.4)
rspec-support (~> 3.0.0)
rspec-expectations (3.0.4)
rspec-support (~> 3.0.0)
rspec-mocks (3.0.4)
rspec-support (~> 3.0.0)
rspec-rails (3.0.2)
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-support (~> 3.0.0)
rspec-support (3.0.4)
spring-commands-rspec (1.0.2)
rspec-rails (~> 3.0.0)
spring-commands-rspec
...
shoulda-matchers (2.7.0)
shoulda-matchers
It seems like it should be pretty straight forward. Am I doing something wrong that's causing me to get this error?
Upvotes: 3
Views: 2216
Reputation: 447
I'm not sure what the root cause is but I just got the same problem - with both bin/rspec and rspec. I managed to get it working for rspec, but not bin/rspec.
I had to add require 'shoulda/matchers'
to the spec file as well as rails_helper.rb to get the test to pass. This passed once, after which I could remove it from the spec file and the test passed. Strange and un-satisfactory fix.
Upvotes: 0
Reputation: 47548
This is due to running the binstub created when installing Spring:
./bin/rspec spec/models/article_spec.rb
Apparently this does not work quite the same way as running the executable installed by RSpec, which is simply:
rspec spec/models/article_spec.rb
Upvotes: 2