Advanced Web Development - AJAX Lab
Aim: The aim of this lab is to understand how to implement AJAX in Rails applications
Instructions:
1. Use the VM supplied in class for this. Please refrain from using your own installs for labs.
2. Please answer all questions - you can be very brief.
3. You may have to refer to the book/slides to understand some things.
1. Develop a RoR application that shows the current time (at the server side) to the user. The index page should have the message "The current time is:
Advanced Web Development - "Unit and Functional Testing Lab"
1. Clone the project from https://github.com/siddharthkaza/depot_p_for_lab.gitunder the lab and import into the IDE.
a. Run the application to make sure it's running in the browser.
Hint: It's always a good idea to run 'bundle update and then 'rake db:migrate' and 'rake db:seed' first - when using an application from another location. This updates any gems needed and brings the DB to its newest version.
1. Validations
Validations are methods that are placed in the model to validate data before records are entered in the database.
In the depot application, we will put in validations in the product model to prevent bad data from being entered in the database (for instance, 'a product should always have a title').
a. Run the depot application, enter a new product in the database (go to /products in the URL to get to the products listing).
b. What validations do you think the product model needs? List them here.
c. Enter the following statements in the product model (product.rb) one-by-one
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
validates :title, length: {minimum: 10}
d. Create another product that fails the validations (for e.g., leave the title empty). You notice that errors show up at the top of the page after you have. How does that work? What data structures are being used in the view to show the errors?
e. Are there any more validations that you want see in the products model? Think about how you would add them. Bring them up when we discuss the lab in the class.
2. Testing
b. Study the model tests for the 'products' model in the application (they are located under the 'tests' folder). Run those model tests and see the results. The command to run all the model tests (also known as 'unit' tests) in your application is 'rake test:units'
Do you think the application needs more unit/model tests for products? Explain here.
c. Study the controller tests in the application. Run the functional tests (rake test:functionals) and see the results.
i. You will notice that some functional tests fail. Why do they fail? Study the assert_difference method for one of them. When one of the test fails you get a message:
Product.count didn't change by -1
<2> expected but was
<4>.
What do the numbers <2> and <4> mean?
ii. Fix the code so that all failing tests pass.
3. Download the address book application https://github.com/siddharthkaza/addressbook_rails42
Write two unit tests and two functional tests for the application. See the examples in depot_p. You might have to write the validations in the model to write unit tests.
4. How much time did you spend on this lab?
Attachment:- Assignment.rar