Hi all,
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:
- whether the user was saved (i.e., if there's a new record in the DB)
- whether his information (name, email, etc.) is correct
- whether a couple of fields have been automatically added to the database before_create
- whether an email was sent
- whether the email text is appropriate (i.e., substitute's the user's name and activation link in the email template)
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.
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.
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) feels more thorough. Which to choose?
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.
Thanks!