[rspec-users] help a new rspec user

ishi soichi soichi777 at gmail.com
Thu Nov 8 05:26:26 UTC 2012


Rails 3.1.3
rspec latest

I have started using rspec a few days ago, although I had been developing
in Rails for a while.

I have developed a Rails application without thinking about tests at all,
but recently realized the importance of BDD.
The application has models and controllers that are already in action.
 They work fine up to this point.  then I installed
rspec and rspec-rails.

I have questions about controller functional test.
The original index of controller is

  def index
    @gives = Give.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @gives }
    end
  end

The generated default controller_spec.rb is

  describe "GET index" do
    it "assigns all gives as @gives" do
      give = Give.create! valid_attributes
      get :index, {}, valid_session
      assigns(:gives).should eq([give])
    end
  end

fixtures/gives.yml is

one:
 check_in: true
 weight:      4
 user_id:     2
 day_departure: "2012-12-01"
 flight_name_id: 3

with the command

 rake db:fixtures:load

If I run command

  bundle exec rspec spec/controllers/gives_controller_spec.rb

All tests fail.
and the error corresponding to index is

  1) GivesController GET index assigns all gives as @gives
     Failure/Error: give = Give.create! valid_attributes
     ActiveRecord::RecordInvalid:
       Validation failed: Flight name can't be blank, Day departure can't
be blank, Check in can't be blank, Weight can't be blank, User can't be
blank
     # ./spec/controllers/gives_controller_spec.rb:39:in `block (3 levels)
in <top (required)>'


Questions:

1. Doesn't rspec generate default test codes so that the test will succeed?
2. Can you guess the cause of the error?
3. Doesn't rspec pick up the data from fixtures automatically? (perhaps I
misunderstand the role of fixtures)

soichi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20121108/b7b094c2/attachment-0001.html>


More information about the rspec-users mailing list