https://github.com/philippmdoerner/mockingbird
A package to mock procedures and functions without turning it all into OOP.
https://github.com/philippmdoerner/mockingbird
mocking-library nim-lang nimscript testing unit-testing
Last synced: 24 days ago
JSON representation
A package to mock procedures and functions without turning it all into OOP.
- Host: GitHub
- URL: https://github.com/philippmdoerner/mockingbird
- Owner: PhilippMDoerner
- Created: 2022-12-27T14:08:59.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-31T12:34:55.000Z (over 2 years ago)
- Last Synced: 2025-03-23T18:50:51.439Z (about 1 month ago)
- Topics: mocking-library, nim-lang, nimscript, testing, unit-testing
- Language: Nim
- Homepage:
- Size: 67.4 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This Package is not finished
#### _Who's mocking who now?_
This is yet another mocking library.
Unlike other mocking libraries, this one allows you to keep your module structure as it is!
Just add the `{.mockable.}` pragma to your exported procs, compile with the `--define:mock` flag and you're good to go!The pragma turns any proc annotated with it into a variable that can be assigned to.
You can then "mock" procs by simply assigning a different procedure to that variable.**Note: This currently does not work with generics! I'm looking into how to get there but it's hard**
# Example
```nim
# get5Module.nim
proc get5*(): int {.mockable.} = 5 # Can be replaced
```
```nim
# add5Module.nim
import ./get5Moduleproc add5*(x: int): int = x + get5()
``````nim
import ./add5Module
import ./get5Module
import std/[sugar, unittest]suite "Test add5":
# Important for the teardown in order to "reset" the mocks
let get5Original = get5
teardown:
get5 = get5Original # "Resets" the mock to provide the normal behaviour againtest """
Given get5 returns 5
When calling add5 with 10
Then it should return 15
""":
check add5(10) == 15test """
Given get5 returns 10
When calling add5 with 10
Then it should return 20
""":
get5 = () => 10
check add5(10) == 20test """
Given get5 not being mocked and so it should returns 5
When calling add5 with 10
Then it should return 15 again
""":
check add5(10) == 15
```