Search code examples
ruby-on-railsrubytestingrspecrspec-rails

How can I automatically generate test cases for Ruby on Rails?


Is there any way to auto-generate simple test cases? I found myself spending time writing very simple tests that make sure all controllers and models are working fine. Here is an example of controller test case written with rspec:

machine = FactoryGirl.create(:machine, type: 1)
mac = FactoryGirl.create(:mac, machine_id: m1.id)
win = FactoryGirl.create(:win, machine_id: m4.id)
sign_in user
get :index
get :show, id: machine.id
get :report

I cannot find any tool today that can auto-generate such tests based on new written code. If really nothing exists, I may consider building a solution to this problem.


Solution

  • To cover basic functionality you could write a specific generator.

    You can also redefine standard scaffold templates (for example, by adding your own template to lib/templates/rspec/model/model_spec.rb - this will redefine model scaffolding).

    But the real question is why to do so. Following TDD you should write the test and then create your code, not vice versa.