Adds some macros used for mocking. Complementary library for bdd-for-c.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # bdd-for-c-mocks
  2. ## Usage
  3. Suppose you have a library `lib` and an application `app` that uses `lib`. You want to test `app` and you want to mock
  4. `lib`'s functions. You can do this by creating a mock header file for `lib` and including it in `app`'s test file.
  5. ### `lib.h`
  6. This is a reference header file for `lib`. The implementation should be abstracted from us, and we want to be able to
  7. mock its behavior.
  8. ```c
  9. #ifndef LIB_H
  10. #define LIB_H
  11. int add(int a, int b);
  12. void alert();
  13. #endif
  14. ```
  15. ### `lib.mock.h`
  16. We define mocked behavior for `lib`. `bdd-for-c-mocks` offers a capability to force mock behavior through modes. Note
  17. that all mode behaviors should be implemented already.
  18. ```c
  19. #ifndef LIB_MOCK_H
  20. #define LIB_MOCK_H
  21. #include <bdd-for-c-mocks.h>
  22. mock_modes(add) {
  23. ADD_RETURNS_SUM = 0,
  24. ADD_RETURNS_CONSTANT_VALUE,
  25. };
  26. mock(add) int add(int a, int b) {
  27. mock_mode_if(add, ADD_RETURNS_SUM) {
  28. // suppose we want to test normal behavior
  29. mock_return(add) a + b;
  30. } else mock_mode_if(add, ADD_RETURNS_CONSTANT_VALUE) {
  31. // maybe an edge case?
  32. mock_return(add) 5;
  33. }
  34. }
  35. mock(alert) void alert() {
  36. mock_return(alert);
  37. }
  38. #endif
  39. ```
  40. ### `app.h`
  41. ```c
  42. #ifndef APP_H
  43. #define APP_H
  44. void math();
  45. #endif
  46. ```
  47. ### `app.c`
  48. This is a reference implementation of `app`. We want to test the invocations done inside the `math()` function.
  49. ```c
  50. #include "lib.h"
  51. #include "app.h"
  52. void math() {
  53. int result_a = add(1, 2);
  54. int result_b = add(3, 4);
  55. if (result_a == result_b) {
  56. // this is the edge case!
  57. // alert() should be called if the add values are the same
  58. alert();
  59. }
  60. }
  61. ```
  62. ### `app.test.c`
  63. This is the test file for `app`. It includes `lib.mock.h` instead of `lib.c`. `lib.h` should still be linked because we
  64. are sharing the original function declarations.
  65. ```c
  66. #include <bdd-for-c.h>
  67. #include "app.h"
  68. spec("app") {
  69. describe("math") {
  70. after_each() {
  71. mock_reset(add);
  72. }
  73. after_each() {
  74. mock_reset(alert);
  75. }
  76. it("calls add") {
  77. mock_mode(add, ADD_RETURNS_SUM);
  78. mock_set_excepted_calls(math, 2);
  79. math();
  80. check(
  81. mock_get_expected_calls(math) == mock_get_actual_calls(math),
  82. "add was not called."
  83. );
  84. }
  85. it("calls alert when calls from add return the same values") {
  86. mock_mode(add, ADD_RETURNS_CONSTANT_VALUE);
  87. math();
  88. check(mock_is_called(alert), "alert was not called.");
  89. }
  90. }
  91. }
  92. ```