Testing Custom Fields Insert
-
Do not go behind testing the core controllers. A better approach is to fabricate the desired object and assert whether the custom fields got added. The reason being, we generally add custom fields on server side
DiscourseEvent
s.- Fabricate the object and call the desired
server side event
with the correct params usingDiscourseEvent.trigger
. - then assert whether the custom fields got added to the object or not.
- Fabricate the object and call the desired
-
Although, do add test for the controllers which you build into the plugin.
Testing custom fields update
- Call the
PostRevisor.revise!
or anyupdated/edited
event applicable explicitly and assert whether the fields got updated properly.
Testing Topic Lists
- If you want to test whether the fields were serialized in the topic list, generate a topic list via
TopicQuery
. Again try to avoid firing a request to/latest.json
etc.
Testing Models(Business Logic)
- The methods created by
ActiveRecord
are already tested well. So Just test the ones created by you.
Serializers
- Create the relevant object(posts/topics/groups/categories) and pass it to
YourSerializer.new
and assert whether your new fields were added to the serializer or not. You may need to pass additional params to the serializer to refer to any official plugin or core discourse which might have a test for that serializer.
Cron Jobs
- The key here is to test the logic. Have a look at core tests on how to test these.
Integration Tests
- These are the tests of the kind, If something happens in the module x, does the module y reacts as expected.
Custom Controllers
- Test the custom controllers built into the plugin by sending requests with correct HTTP Request Type(get, post, put etc.) and assert the correctness of the response.
General Advise
- Focus on a single class/method at a time.
More coming in this space(code examples and gotchas).