Question

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.

Was it helpful?

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.

OTHER TIPS

If a test were predictable enough to generate it wouldn't be worth writing. In your example, you don't assert anything. That's a very weak test, good only for raising code coverage. It would be much stronger if it asserted what should be on the page. You can't generate that. You also can't generate a scenario that traverses multiple pages in a meaningful way. (I think your example wants to be an rspec feature spec or a Cucumber scenario.)

It would make sense to write a generator that creates a skeleton that the developer could fill in with the meaningful parts that can't be generated, however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top