Add Factorygirl as an alternative to fixtures#1279
Conversation
Since the database also contains fixtures from other tests, some counts are dropped and instead tested for inclusion in the results.
| api_fixtures | ||
| fixtures :diary_comments | ||
|
|
||
| def test_diary_comment_count |
There was a problem hiding this comment.
This test name doesn't really reflect what the test does any more... Then again I'm not really sure what it is testing now - is it actually just testing that FactoryGirl can create records? or does that itself funnel through the rails code so that we're testing rails can create records?
|
Thanks for the comments. One of the problems is that the original tests are a bit pointless - they are mostly asserting that the fixtures have loaded, or that Rails itself is working (e.g. build a model from attributes, whatdoyouknow, the model has those attributes). The refactored versions are still mostly trivial, and often a bit pointless too. I would prefer to rewrite most of these tests completely to test our "business logic" instead. But I was wary of trying to do too many things at once, since I'm always suspicious when people start rewriting tests instead of refactoring them! I'll fix the specific points you've raised, and make a separate PR with some more thorough rewriting. |
|
Yes I always find that a lot of model tests involve a fair bit of "what exactly are we really testing here" head scratching... I mean obviously if you have a method with some additional logic in it on a model that's fine and can be tested but a lot of the time the tests as you say seem to be testing rails more than our code. I think things that make sense in a model test are:
On the other handing, testing that |
FactoryGirl is a widely used alternative to fixtures in tests. The fixtures are annoying since every time you want to test something slightly different (for example, a diary entry with an apostrophe in its title), you need to add a whole new fixture and rework the tests. Fixtures are also a pain when the model attributes change.
This PR adds FactoryGirl, and reworks two model tests (DiaryEntry and DiaryComment) to use them.