Code Monkey home page Code Monkey logo

Comments (6)

ruralocity avatar ruralocity commented on July 17, 2024 2

Hey @dimanyc, thanks for helping out here!

from rails-4-1-rspec-3-0.

dimanyc avatar dimanyc commented on July 17, 2024

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.

romenigld avatar romenigld commented on July 17, 2024

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.

dimanyc avatar dimanyc commented on July 17, 2024

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.

romenigld avatar romenigld commented on July 17, 2024

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.

romenigld avatar romenigld commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.