Stubr - Now Records Call Information

Stubr now records information about function calls. In particular, the inputs and outputs of a function are recorded in the order that the function is called.

To facilitate this, each stubbed module has a function called __stubr__ and this is used to get information about the stub.

For example, to get information about a particular function call, you can call the __stubr__ function like so:

stubbed = Stubr.stub!(Float, [
  {:ceil, fn 0.8 -> :stubbed_return end},
  {:parse, fn _ -> :stubbed_return end},
  {:round, fn(_, 1) -> :stubbed_return end},
  {:round, fn(1, 2) -> :stubbed_return end}
], auto_stub: true)

stubbed.ceil(0.8)
stubbed.parse("0.3")
stubbed.round(8, 1)
stubbed.round(1, 2)
stubbed.round(1.2)
stubbed.round(1.324, 2)
stubbed.ceil(1.2)
stubbed.ceil(1.2345, 2)
stubbed.to_string(2.3)

assert stubbed.__stubr__(call_info: :ceil) == [
  %{arguments: [0.8], output: :stubbed_return},
  %{arguments: [1.2], output: 2.0},
  %{arguments: [1.2345, 2], output: 1.24}
]

assert stubbed.__stubr__(call_info: :parse) == [
  %{arguments: ["0.3"], output: :stubbed_return}
]

assert stubbed.__stubr__(call_info: :round) == [
  %{arguments: [8, 1], output: :stubbed_return},
  %{arguments: [1, 2], output: :stubbed_return},
  %{arguments: [1.2], output: 1.0},
  %{arguments: [1.324, 2], output: 1.32}
]

assert stubbed.__stubr__(call_info: :to_string) == [
  %{arguments: [2.3], output: "2.3"}
]

You can find Stubr on hex and github.

2 Likes