Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for testing Models in Rails?

How much or how little should one test models in rails? Since the framework is basically doing so much for you, I'm wondering if it's worth testing the generated activerecord methods to make sure they work or not.

Do you guys test them implicitly through the controllers?

In the Java world, if I used Hibernate, I had to write so much ORM mapping stuff that testing save/delete/find for each entity was VERY important - even if I just inherited these methods from a base class - because it was easily possible to get the mapping information wrong, or make a silly mistake. Deletes were important to test to make sure Hibernate would cascade properly.

But since you don't configure any of this stuff in rails at all... it's just so simple... is it worth testing, or do you just assume it works? Do you just limit yourself to maybe making sure associations work as expected?

What is the best practice? Any good testing examples? I found a page here: http://guides.rubyonrails.org/testing.html

But it didn't talk specifically about ActiveRecord. It focused more on controllers and other things.

Thanks!

like image 359
egervari Avatar asked May 01 '11 01:05

egervari


People also ask

What are system tests in Rails?

A Rails 6 system test is a test that exercises your application in a way that, as much as possible, simulates a real user interacting with it via a browser. More than any other kind of tests, system tests verify that the whole app does what it's supposed to do.

What is Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.

How do you test a method in Ruby?

Most methods can be tested by saying, “When I pass in argument X, I expect return value Y.” This one isn't so straightforward though. This is more like “When the user sees output X and then enters value V, expect subsequent output O.” Instead of accepting arguments, this method gets its value from user input.


1 Answers

I don't try to test any methods introduced by active record and such. They are already tested by the rails team. And they already have written test cases.

but i would test all my methods and basically any custom code that i write in my models. First to test them as individual units i will use unit tests. And to make sure the whole thing works (models controllers..etc) you can use integration tests.

the railsguides would be a very good starting point for you. afterwards you can digg in to more interesting stuff. i would recommend you to take a look and rspec and cucumber.

  • Cucumber
  • Rspec

Some more stackoverflow questions (and answers) that would be helpful to you

  • Getting Started with RSpec - Looking for tutorials
  • What is Unit test, Integration Test, Smoke test, Regression Test?
like image 139
thekindofme Avatar answered Oct 13 '22 12:10

thekindofme