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>
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 > endYour 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
thanks. I will look for Factory gems. soichi 2012/11/9 Cynthia Kiser <cnk at ugcs.caltech.edu>> 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20121109/136cb355/attachment.html>