Skip to content

Add Factorygirl as an alternative to fixtures#1279

Merged
tomhughes merged 5 commits into
openstreetmap:masterfrom
gravitystorm:factorygirl
Sep 8, 2016
Merged

Add Factorygirl as an alternative to fixtures#1279
tomhughes merged 5 commits into
openstreetmap:masterfrom
gravitystorm:factorygirl

Conversation

@gravitystorm

Copy link
Copy Markdown
Collaborator

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.

Since the database also contains fixtures from other tests, some
counts are dropped and instead tested for inclusion in the results.
Comment thread test/models/diary_comment_test.rb Outdated
api_fixtures
fixtures :diary_comments

def test_diary_comment_count

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@gravitystorm

Copy link
Copy Markdown
Collaborator Author

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.

@tomhughes

tomhughes commented Sep 8, 2016

Copy link
Copy Markdown
Member

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:

  • That any additional methods written by us work
  • That associations return the expected records, especially where they involved filtering, ordering, etc
  • That validations allow/reject the correct things

On the other handing, testing that count returns the right number of records is probably less useful ;-)

@tomhughes tomhughes merged commit 191f6b3 into openstreetmap:master Sep 8, 2016
@gravitystorm gravitystorm deleted the factorygirl branch October 19, 2016 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants