public void QuickerVerification() { // Quickly Verify that a mock member was never called _moq.Verify <ILeaf>(x => x.Grow()).Never(); // Or that it was called once _moq.Of <ILeaf>().Object.Grow(); _moq.Verify <ILeaf>(x => x.Grow()).Once(); // Or called X number of times _moq.Of <ILeaf>().Object.Grow(); _moq.Verify <ILeaf>(x => x.Grow()).Times(2); // Or fallback to using the Moq.Times class _moq.Of <ILeaf>().Object.Grow(); _moq.Verify <ILeaf>(x => x.Grow()).Times(Times.AtLeast(3)); _moq.Verify <ILeaf>(x => x.Grow()).Times(Times.Between(3, 7, Range.Inclusive)); }
public void DoWork_CallsRepositoryGetData() { // 1 - First create an instance of Moqqer which will // act as a container for Mocks and Objects. _moq = new Moqqer(); // 2 - Get Moqqer to create an instance of some class you want to test. // It will auto inject mocks in the its constructor. _subject = _moq.Create <SomeClass>(); // 3 - Call the mothod you want to test on SomeClass _subject.CallA(); // Calls private field IDependencyA.Call() // 4 - Verify a mock that was auto injected was called. _moq.Of <IDepencyA>() .Verify(x => x.Call(), Times.Once); // Alternatively use the Verify extension method _moq.Verify <IDepencyA>(x => x.Call()) .Once(); // 5 - Test and Refactor to your hearts content // without worrying about changing the constructor calls! }
public void Verify_Once_MethodNotCalled_Exception() { Action action = () => _moq.Verify <ILeaf>(x => x.Grow()).Once(); action.ShouldThrow <MockException>() .WithMessage("*Expected invocation on the mock once, but was 0 times*"); }