[rspec-users] help a new rspec user

Cynthia Kiser cnk at ugcs.caltech.edu
Thu Nov 8 15:41:22 UTC 2012


Quoting ishi soichi <soichi777 at gmail.com>:
> 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

Your test above is creating a new give with the parameters in
valid_attributes - which is also generated for you in that same file
but will be an empty hash because the rspec generator doesn't know
anything about your problem domain. 

The fixtures file you quoted isn't being used to create the object
under test in the example above. 

> 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)

I think you could use fixtures with rspec if you work at it. You might
need to load them explicitly as I don't know that the rspec rails
helper would load them for you. And then you need to reference them
using the correct fixture access syntax (which I am too lazy to look
up). But to get your generated tests running quickly, ignore fixtures
and just fill in the valid_attributes hash in the generated rspec
tests OR drop in a factory gem and start using factories. I personally
use FactoryGirl but there are several very nice factory gem that work
well with Rails. 


-- 
Cynthia N. Kiser
cnk at ugcs.caltech.edu


More information about the rspec-users mailing list