One day i saw a snippet of code inside controller test, which is green and sadly is a little hard to understand:
After a while, i realized this tests are trying to assert query invoked in an index action. Unfortunately at first sight it doesn’t have a clear intent about what we are going to test.
Besides that this test is not integrated with the views that will be rendered. The test still pass against this code below, if we forgot to supply view variable:
Ok, we have a weak controller test.
I think there are two solutions, first is refactor the controller test to be more clear intent and tight, second is tell a controller example to render views.
- We can apply arrange-act-assert (AAA) pattern for this test and add more assertion. AAA is simple concept: setup context -> execute thing -> verify expected behavior.
- Tell controller example to render view using render_views method and add more assertion with view.