Comments (6)
Hey @dimanyc, thanks for helping out here!
from rails-4-1-rspec-3-0.
It looks like contact
instances are created elsewhere in your code, since actual collection contained
includes other objects besides smith
and jones
. Your spec is failing because match_array
will will be true only if the array at hand contains the objects you specify and nothing else. So:
expect([1, 2, 3]).to match_array([3, 2, 1]) # will return true
expect([1, 2, 3, 4]).to match_array([3, 2, 1]) # will return false
You can either figure out where these additional instances are being created and clean them with something like database_cleaner, or use include
instead of match array
:
expect(assigns(:contacts)).to include(smith, jones)
from rails-4-1-rspec-3-0.
I'm following and typing the samples for a better recording.
So I try to put like 'String' and then the sample include and was the same error.
So when I try many times I have a different error. And I don't know what is occurring maybe is something in the core of the RSpec. Try to config the database_cleaner. And this error persists:
$ git branch
01_untested
02_setup
03_models
* 04_factories
05_controller_basics
master
$ rspec spec/controllers/contacts_controller_spec.rb§
/Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/configuration.rb:1105:in `load': cannot load such file -- /Users/romenigld/workspace/ebooks/everyday_rails_testing_with_rspec/rails-4-1-rspec-3-0/spec/controllers/contacts_controller_spec.rb§ (LoadError)
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/configuration.rb:1105:in `each'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/runner.rb:96:in `setup'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/runner.rb:84:in `run'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/runner.rb:69:in `run'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/runner.rb:37:in `invoke'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/exe/rspec:4:in `<top (required)>'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/bin/rspec:23:in `load'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/bin/rspec:23:in `<main>'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/romenigld/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
from rails-4-1-rspec-3-0.
Try adding a before
block to remove all records of contact
prior to creating smith
and jones
describe 'GET #index' do
before(:each) do
Contact.destroy_all
end
...
context 'without params[:letter]' do
it "populates an array of all contacts" do
smith = create(:contact, lastname: 'Smith')
jones = create(:contact, lastname: 'Jones')
get :index
expect(assigns(:contacts)).to match_array([smith, jones])
end
...
As far as setting up database cleaner, use their README Rspec example. If you are still having issues, post a link to your repo here.
from rails-4-1-rspec-3-0.
It's complaining this error that I show you before. It's started of nowhere as I testing this problem, it's very strange.
error:
rspec spec/controllers/contacts_controller_spec.rb§
/Users/romenigld/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.1.3/lib/rspec/core/configuration.rb:1105:in `load': cannot load such file -- /Users/romenigld/workspace/ebooks/everyday_rails_testing_with_rspec/rails-4-1-rspec-3-0/spec/controllers/contacts_controller_spec.rb§ (LoadError)
from rails-4-1-rspec-3-0.
I do a fool commit and so I change the branch for the 05 chapter. Then the error persists. So I run the bundle install. And when I try the rspec command, the rspec command works. So i git checkout for the 04 chapter branch.
Do the same I run the bundle install. Then with your before block for delete_all. works fine!!
ContactsController
GET #show
assigns the requested contact to @contact
renders the :show template
GET #index
with params[:letter]
populates an array of contacts starting with the letter
renders the :index template
without params[:letter]
populates an array of all contacts
renders the :index template
GET #new
assigns a new Contact to @contact
renders the :new template
GET #edit
assigns the requested contact to @contact
renders the :edit template
POST #create
with valid attributes
saves the new contact in the database
redirects to contacts#show
with invalid attrubutes
does not save the new contact in the database
re-renders the :new template
Finished in 0.60853 seconds (files took 2.32 seconds to load)
14 examples, 0 failures
So I will continue the ebook. Thank's for reply!!
from rails-4-1-rspec-3-0.
Related Issues (20)
- Well done HOT 1
- Shared db connection
- Ch.9 Avoid useless stubbing
- Ch.5 assigns[key] is used
- Ch 9. How about refactoring contacts_controller_spec ?
- Ch 9. set_user_session is not used
- Ch 8. Feature spec works without find method
- Ch 6. id is not required for create
- Ch.5 be_true should be be_truthy
- Where to place `gem "factory_girl_rails"` in Gemfile HOT 6
- application.rb p. 17 HOT 2
- Can't pass `bundle exec spec` on 05_controller_basics branch HOT 3
- error with invalid contact for controller spec HOT 3
- CH.5 Failing Test HOT 1
- selenium-webdriver won't work against Firefox 48 or later HOT 2
- [error] newer bundler causes: Your Gemfile.lock is corrupt. The following gem is missing from the DEPENDENCIES section: 'rspec' HOT 1
- Chapter 5 Testing Controllers HOT 1
- Question on controller testing advanced..authentication comment out... HOT 1
- 09_speedup "ruby_dep requires Ruby version >= 2.2.5, ~> 2.2." HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails-4-1-rspec-3-0.