Search code examples
ruby-on-railsrspecfunctional-testing

Is it cheating to override default settings when writing functional tests?


I'm writing some tests for a rails application to make sure my index action is paginating the results. I didn't want to create 25+ objects for the test (default items per page is 25), so I changed the default_per_page parameter in my test to a much more reasonable (for testing purposes) 2.

Does this violate any best-practice rules I'm not thinking of? It sits right on the line of clever hack and shameful kludge in my mind.


Solution

  • Clearly Define the Test Objective

    I think the word "cheating" is a bit misleading, as the tests are for your benefit in exercising and maintaining the code. To that end, anything that accomplishes your test objectives isn't really cheating. The real question is, what is your actual objective?

    Exercise Behavior, Not Implementation

    Let's assume that your test objective is to ensure that pagination should split records across pages. Perhaps your test says:

    it 'should successfully paginate records into 2 pages' do
      # test that pagination creates two pages
    end
    

    In that case, making the test simpler by reducing fixture/factory setup clarifies the test. You aren't testing that you have 25 records on the first page; you are just testing that pagination splits results into two pages. You have framed the test according to expected behavior, and defined a non-brittle test that provides flexibility in case you paginate differently in the future.

    On the other hand, if you rolled your own custom pagination code instead of using a widely-used and battle-tested pagination gem, then you should certainly be testing all the boundary conditions, rather than just a single use case of 25 records per page. A single test is certainly inadequate to test all the relevant permutations here, so if that's your actual test objective, you will need to broaden your tests accordingly.