| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
Handling Publishers in a Book Inventory Management System(Page 1 of 2 ) In this third part of a multi-part series on the scaffolding feature of Ruby on Rails, we'll pick up where we left off last time, and complete the add publisher user story and the view publisher user story. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818). Completing the Add Publisher User Story The scaffolding script has created a functional test in test/functional/admin/publisher_controller_test.rb. On closer inspection, we can see that it requires some modifications for it to be helpful in our efforts at producing bug-free code. For example, the test_createmethod doesn’t specify a name for the publisher it creates. This should have made the test fail when we ran the test, but it didn’t. We will fix that soon, but the first thing we will do is add validations. You never know what kind of data a user will try to enter into your application. As explained in the previous chapter, validations help ensure that only valid data is inserted into the database. Adding Validations to the Model Adding validations will make the test fail and show you where the parameters should be specified. So let’s begin by adding a validation for thenamefield in thePublishermodel. Openapp/models/publisher.rband modify it to look as follows: class Publisher < ActiveRecord::Base As you might remember, we specified the maximum length of thenamefield in thepublisherstable to be 255 characters. We add a validation to the model to verify this constraint and specify that the minimum length of thenamefield to be 2 characters. We also add a validation that checks that the publisher name is unique. Modifying the Generated Fixture Data The fixture data generated by scaffolding is not very descriptive of our project. We can do better. Opentest/fixtures/publishers.ymlin your editor and remove everything from the file. Then add the following: apress: Again, execute the functional tests withrake test:functionals. You should see the tests fail with the following error message: -------------------------------------------- The test fails as expected because thetest_createmethod doesn’t provide any parameters to thepost method that is supposed to create the publisher. To fix this, we’ll change the functional test. Modifying the Generated Functional Test Opentest/functional/admin/publisher_controller_test.rbin your editor and change thetest_createmethod as follows: def test_create post :create, :publisher => {:name => 'The Monopoly Publishing Company'} assert_response :redirect assert_equal num_publishers + 1, Publisher.count The only change is that we pass a hash to thepost method, instead of no data at all. The hash contains the name for the publisher the test should create. Run the tests again, and you should see the functional test pass. You should also do a quick test in the browser to verify that the Add Publisher user story functionality works. Openhttp://localhost:3000/admin/publisher/newin your browser. Test the validations you just added by clicking the Create button, without specifying a name. You should see the error message shown in Figure 3-2. This error message is automatically generated by Rails, and explains exactly what you should do in order to fix the error.
We further examine the functional tests and see that those for the List Publishers and Delete Publisher user stories are satisfactory. However, the test for the View Publisher and Edit Publisher user stories require some work. More Database Articles Articles |
| |
| |