开发者

Testing: insert value passed into Action<T>

开发者 https://www.devze.com 2023-02-17 00:41 出处:网络
How do you use Rhino Mocks to insert a value into an action? Register() takes an action that is assigned to a local variable that is used later in the function:

How do you use Rhino Mocks to insert a value into an action?

Register() takes an action that is assigned to a local variable that is used later in the function:

var result = EnumResult.Timeout;

something.Register<EnumResult>(r => result = r);

<do things with re开发者_运维百科sult which need to be unit tested>

I want to be able to inject a value as r into the action (as it is defined in the function) and test what happens afterwards.


Couldn't you just pass a lambda in your test that sets some variable and then validate the variable was set at the end of the test? Something like this:

var wasCalled = false;
mock.Stub(s => s.Register(r => wasCalled = true);
...
Assert.IsTrue(wasCalled);

This will ensure your "Register" method is calling the lambda that is passed to it.

0

精彩评论

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