Completed
Last Updated: 14 Oct 2015 08:23 by Stefan
Kaloyan
Created on: 21 Aug 2013 08:44
Type: Feature Request
3
A Mock.Create behavior, that will arrange all functions to OccursNever automatically instead of throwing exceptions
We have some mission critical code that catches all exceptions and recovers from them in various ways.  I would like to be able to use Mock.Create<MyClass>(Behavior.Strict) so that I can know that none of the methods on MyClass are being called besides the ones I explicitly Mock.Arrange.  However, this results in the methods throwing exceptions which are then caught by my application and recovered from so I never see them.

I would like something like this, but where I didn't have to manually arrange every method on the class and instead have some Behavior that I could give to Mock.Create that would result in all of the arranges being auto-generated.  I could then manually arrange anything I didn't want to have OccursNever on, just like you can override the exceptions thrown by Behavior.Strict.


class MyClass
{
    public void Method1() { }
    public void Method2() { }
    public void Method3() { }
}
 
class ClassUnderTest
{
    public void DoSomething(MyClass myClass)
    {
        myClass.Method3();
    }
}
 
[Test]
void MyClass_methods_are_never_called()
{
    // ARRANGE
    var myClass = Mock.Create<MyClass>();
    Mock.Arrange(() => myClass.Method1()).OccursNever();
    Mock.Arrange(() => myClass.Method2()).OccursNever();
    Mock.Arrange(() => myClass.Method3()).OccursNever();
 
    // ACT
    var classUnderTest = new ClassUnderTest();
    classUnderTest.DoSomething(myClass);
 
    // ASSERT
    Mock.Assert(myClass); // this will fail
}
1 comment
Stefan
Posted on: 14 Oct 2015 08:23
Strict mock violations are now also reported when asserting the mock. Try it out and see if it works well for you.