Objective: Use TDD in Rails to create an inventory management application. Your goal is to write code to pass the tests.
- Fork this repo, and clone it into your
develop
folder on your local machine. - Run
bundle install
to install gems. - Run
rake db:create db:migrate
to create and migrate the database. - Start your Rails server.
- Run
rspec
in the Terminal. You should see an angry error message. Your job is to fix it!
- The failing specs are for a
ProductsController
. For the first part of this lab, implement the functionality for theProductsController
to pass the tests. Some tips:- Read the errors carefully. They will guide you as to what to do next.
- Once you've gotten past the initial setup errors, and you have failing specs printing out in the Terminal, it may help to only run specific specs by name using
rspec spec -e '#index'
- You DON'T need to implement fully-functioning views, but you can if you want to.
- Once you have all the specs passing for the
ProductsController
, it's time to implement a unit test for products:- Products should have an instance method called
#margin
that calculates the retail margin. - Write the spec for
#margin
before implementing the method! - Hint:
rails g rspec:model product
- Products should have an instance method called
Feel free to reference the solution branch for guidance.
- A product should have many items. Use TDD to guide your implementation of CRUD for items. Follow the examples in
spec/controllers/products_controller_spec.rb
as a guide for testing yourItemsController
. - Items should have a minimum of three attributes:
size
,color
, andstatus
. Validate these three attributes forpresence
. - Items routes should be nested under products routes. See the Rails docs for nested resources.
- Your
ItemsController
doesn't need an#index
method, since your app should display all of a product's items on theproducts#show
page. However, it should have the other six methods for RESTful routes (#new
,#create
,#show
,#edit
,#update
, and#destroy
). - You DON'T need to implement fully-functioning views, but you can if you want to.
- Take advantage of the factory_girl_rails and ffaker gems to define an
item
factory to use in your tests. - Once you have passing specs for your
ItemsController
, it's time for another unit test:- Products should have an instance method called
#sell_through
that calculates the sell-through rate (items sold / total items). - Write the spec for
#sell_through
before implementing the method!
- Products should have an instance method called
Feel free to reference the solution_items branch for guidance.