Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Created June 24, 2014 12:32
Show Gist options
  • Save urasandesu/d245668bec19d93775f9 to your computer and use it in GitHub Desktop.
Save urasandesu/d245668bec19d93775f9 to your computer and use it in GitHub Desktop.
How to use Prig in combination with NSubstitute.
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);
}
}
}
}
@urasandesu
Copy link
Author

NotifyingObject class is same as this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment