Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere/what to test? Mocks, stubs, functional, unit... (in Ruby/Rails)?
    text
    copied!<p>I'm trying to test a fairly large Rails app, which I probably should have been doing all along but never felt entirely comfortable with. Now I'm working on a functional test to see if a user is created successfully. Basically, I'd like to test a few things:</p> <ul> <li>whether the user was saved (i.e., if there's a new record in the DB)</li> <li>whether his information (name, email, etc.) is correct</li> <li>whether a couple of fields have been automatically added to the database before_create</li> <li>whether an email was sent</li> <li>whether the email text is appropriate (i.e., substitute's the user's name and activation link in the email template)</li> </ul> <p>As I understand it, unit tests are designed to test models and functional tests should test controllers. Several of the tests I mention above should be unit tests, as I see it -- specifically, I think I can make sure that inputs are correctly mapped to database fields and that the before_create filter works in a unit test with a User model.</p> <p>Others seem to call for functioanl tests -- whether the email is sent (and possibly its text -- although maybe that belongs in a UserEmail test?) and whether there's a new record.</p> <p>Here's the question about mocking/stubbing. In the user controller functional test, should I just make sure that user.save is called with the appropriate parameters, or should I test that the DB gets a new record? The former seems to call for a stub and assumes that, since Rails is so well tested, the object will be saved successfully if .save is called on the user model. But the latter (e.g., calling assert_difference) <em>feels</em> more thorough. Which to choose?</p> <p>Sorry if this is too involved a question -- I understand it's a big topic, but my hope is that understanding a specific (if long) example like this will clarify a whole lot about testing in general.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload