Hi Jonathan! Thanks for pasting in that gist. And awesome you were able to find the solution yourself! I don’t do a lot of controller testing (mostly unit tests and integration tests using capybara and capybara-webkit) but here are a few ideas for how to improve the tests above:
Mix the FactoryGirl syntax methods in by including the following in your spec_helper.rb file:
RSpec.configure do |config|
That way, you can remove all of the instance of ‘FactoryGirl.’ in this file and all other spec files.
Clean up naming
It wasn’t clear to me why the following expectations are expected in your first spec:
This isn’t necessarily because you have a bad test. It is more likely that the naming of "test scenario’ and “another_test_scenario” is not revealing why it should be be included. When looking at the setup, I see that the scenario is using a factory called ‘test_scenario.’ How is this factory different from a real scenario? Does your project have a model called test scenario or is this factory creating a scenario with different traits? If the latter is the case, I would consider taking advantage of FactoryGirl’s traits: http://robots.thoughtbot.com/post/23673635798/remove-duplication-with-factorygirls-traits
- Add setup within each spec
For the test with the describe block of
`it 'returns real scenarios for a specific user' do
it is a little confusing that the expectations are the similar to the expectations of your first spec that does not include a user. When looking at the setup, it makes more sense. This is why I generally prefer to include my setup within each test. If you did that, you could create one user and two scenarios for that particular test, one named
user_scenario and the other named
other_user_scenario, which would belong to another user, and include the following expectations:
If you went with this method (including setup within each spec and naming factory objects more explicitly), it would also be helpful to separate setup, exercise, verification, and teardown phases with newlines.
Hope these ideas help you, Jonathan! Have a great day.