开发者

Unit testing processes that use records for state

I'd like to unit test a gen_fsm that uses a fairly large record for its state. The record is defined within the erl file that also defines the gen_fsm and thus is not (to my knowledge) visible to other modules.

Possible approaches:

  1. Put the record into an hrl file and include that in both modules. This is ok, but spreads code that is logically owned by the gen_fsm across multiple files.
  2. Fake a record with a raw tuple in the unit test module. This would get pretty ugly as the record is already over 20 fields.
  3. Export a function from my gen_fsm that will convert a proplist to the correct record type with some record_info magic. While possible, I don't like the idea of polluting my module interface.
  4. Actually spawn the gen_fsm and send it a series of messages to put it in the right state for the unit test. There is su开发者_JS百科bstantial complexity to this approach (although Meck helps) and I feel like I'm wasting these great, pure Module:StateName functions that I should be able to call without a whole bunch of setup.

Any other suggestions?


You might consider just putting your tests directly into your gen_fsm module, which of course would give them access to the record. If you'd rather not include the tests in production code, and assuming you're using eunit, you can conditionally compile them in or out as indicated in the eunit user's guide:

-ifdef(EUNIT).
   % test code here
   ...
-endif.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜