Created
June 24, 2014 12:32
-
-
Save urasandesu/d245668bec19d93775f9 to your computer and use it in GitHub Desktop.
How to use Prig in combination with NSubstitute.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NSubstitute; | |
using NUnit.Framework; | |
using System; | |
using System.ComponentModel; | |
using System.Prig; | |
using Urasandesu.Prig.Framework; | |
namespace MockingDelegatesSample | |
{ | |
[TestFixture] | |
public class NSubstituteTest | |
{ | |
[Test] | |
public void ValueWithRandomAdded_should_raise_PropertyChanged_event_with_its_name() | |
{ | |
// Arrange | |
var notifyingObject = new NotifyingObject(); | |
var mockHandler = Substitute.For<PropertyChangedEventHandler>(); | |
mockHandler(Arg.Any<object>(), Arg.Any<PropertyChangedEventArgs>()); | |
notifyingObject.PropertyChanged += mockHandler; | |
// Act | |
notifyingObject.ValueWithRandomAdded = 42; | |
// Assert | |
mockHandler.Received(1)(notifyingObject, Arg.Is<PropertyChangedEventArgs>(o => o.PropertyName == "ValueWithRandomAdded")); | |
} | |
[Test] | |
public void ValueWithRandomAdded_should_hold_passed_value_plus_random_value() | |
{ | |
using (new IndirectionsContext()) | |
{ | |
// Arrange | |
var notifyingObject = new NotifyingObject(); | |
var mockNext = Substitute.For<IndirectionFunc<Random, int>>(); | |
mockNext(Arg.Any<Random>()).Returns(10); | |
PRandom.Next.Body = mockNext; | |
// Act | |
notifyingObject.ValueWithRandomAdded = 32; | |
var actual = notifyingObject.ValueWithRandomAdded; | |
// Assert | |
mockNext.Received(1)(Arg.Any<Random>()); | |
Assert.AreEqual(42, actual); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NotifyingObject class is same as this example.