Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Replaced Moq with NSubstitute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Graeme Foster authored and GraemeF committed Feb 16, 2011
1 parent 0d182c5 commit 6527ef8
Show file tree
Hide file tree
Showing 46 changed files with 550 additions and 16,269 deletions.
13 changes: 5 additions & 8 deletions Twiddler.Tests/Commands/DeauthorizeCommandTests.cs
Expand Up @@ -3,9 +3,8 @@ namespace Twiddler.Tests.Commands
#region Using Directives

using System;
using System.ComponentModel;

using Moq;
using NSubstitute;

using Twiddler.Commands;
using Twiddler.Core.Services;
Expand All @@ -17,7 +16,7 @@ namespace Twiddler.Tests.Commands

public class DeauthorizeCommandTests
{
private readonly Mock<IAuthorizer> _fakeClient = new Mock<IAuthorizer>();
private readonly IAuthorizer _client = Substitute.For<IAuthorizer>().WithReactiveProperties();

[Fact]
public void CanExecuteChanged_WhenAuthorizationStatusChanges_IsRaised()
Expand Down Expand Up @@ -65,19 +64,17 @@ public void Execute__DeauthorizesClient()

test.Execute(null);

_fakeClient.Verify(x => x.Deauthorize());
_client.Received().Deauthorize();
}

private DeauthorizeCommand BuildDefaultTestSubject()
{
return new DeauthorizeCommand(_fakeClient.Object);
return new DeauthorizeCommand(_client);
}

private void ClientAuthorizationStatusChangesTo(AuthorizationStatus status)
{
_fakeClient.Setup(x => x.AuthorizationStatus).Returns(status);
_fakeClient.Raise(x => x.PropertyChanged += null,
new PropertyChangedEventArgs("AuthorizationStatus"));
_client.PropertyChanges(x => x.AuthorizationStatus, status);
}
}
}
8 changes: 4 additions & 4 deletions Twiddler.Tests/Commands/MarkTweetAsReadCommandTests.cs
Expand Up @@ -2,7 +2,7 @@
{
#region Using Directives

using Moq;
using NSubstitute;

using Twiddler.Commands;
using Twiddler.Core.Models;
Expand All @@ -15,7 +15,7 @@

public class MarkTweetAsReadCommandTests
{
private readonly Mock<ITweetStore> _fakeStore = new Mock<ITweetStore>();
private readonly ITweetStore _store = Substitute.For<ITweetStore>();

private readonly ITweet _tweet = A.Tweet.Build();

Expand Down Expand Up @@ -65,12 +65,12 @@ public void Execute_WhenTweetIsNotRead_SavesTweetToStore()

test.Execute(null);

_fakeStore.Verify(x => x.Add(_tweet));
_store.Received().Add(_tweet);
}

private MarkTweetAsReadCommand BuildDefaultTestSubject()
{
return new MarkTweetAsReadCommand(_tweet, _fakeStore.Object);
return new MarkTweetAsReadCommand(_tweet, _store);
}
}
}
@@ -1,4 +1,4 @@
namespace Twiddler.Tests.Screens
namespace Twiddler.Tests
{
#region Using Directives

Expand All @@ -7,34 +7,36 @@
using System.ComponentModel;
using System.Linq.Expressions;

using Moq;
using NSubstitute;

using ReactiveUI;

#endregion

public static class ReactiveObjectMockHelpers
{
public static void PropertyChanges<T, TValue>(this Mock<T> fake,
public static void PropertyChanges<T, TValue>(this T substitute,
Expression<Func<T, TValue>> expression,
TValue newValue)
where T : class, IReactiveNotifyPropertyChanged
{
string propertyName = GetPropertyName(expression);
T sender = fake.Object;
T sender = substitute;

PublishChange(sender, sender.Changing, propertyName, newValue);

UpdatePropertyValue(expression, fake, newValue);
RaisePropertyChanged(fake, propertyName);
UpdatePropertyValue(expression, substitute, newValue);
RaisePropertyChanged(substitute, propertyName);

PublishChange(sender, sender.Changed, propertyName, newValue);
}

public static void SetupReactiveObject<T>(this Mock<T> fake) where T : class, IReactiveNotifyPropertyChanged
public static T WithReactiveProperties<T>(this T substitute) where T : class, IReactiveNotifyPropertyChanged
{
SetupChangeSubject(fake, x => x.Changing);
SetupChangeSubject(fake, x => x.Changed);
substitute.Changing.Returns(new Subject<IObservedChange<object, object>>());
substitute.Changed.Returns(new Subject<IObservedChange<object, object>>());

return substitute;
}

private static string GetPropertyName<T, TValue>(Expression<Func<T, TValue>> expression)
Expand All @@ -56,30 +58,19 @@ public static class ReactiveObjectMockHelpers
});
}

private static void RaisePropertyChanged<T>(Mock<T> fake, string propertyName)
private static void RaisePropertyChanged<T>(T substitute, string propertyName)
where T : class, INotifyPropertyChanged
{
fake.Raise(x => x.PropertyChanged += null,
new PropertyChangedEventArgs(propertyName));
}

private static void SetupChangeSubject<T>(Mock<T> fake,
Expression<Func<T, IObservable<IObservedChange<object, object>>>> expression)
where T : class
{
fake.
Setup(expression).
Returns(new Subject<IObservedChange<object, object>>());
substitute.PropertyChanged +=
Raise.Event<PropertyChangedEventHandler>(new PropertyChangedEventArgs(propertyName));
}

private static void UpdatePropertyValue<T, TValue>(Expression<Func<T, TValue>> property,
Mock<T> fake,
T substitute,
TValue newValue)
where T : class
{
fake.
Setup(property).
Returns(newValue);
property.Compile()(substitute).Returns(newValue);
}
}
}
4 changes: 2 additions & 2 deletions Twiddler.Tests/Screens/ImageThumbnailScreenTests.cs
Expand Up @@ -4,7 +4,7 @@

using System;

using Moq;
using NSubstitute;

using Twiddler.Commands.Interfaces;
using Twiddler.Models;
Expand All @@ -23,7 +23,7 @@ public class ImageThumbnailScreenTests
Thumbnail = new Uri("http://thumbnail")
};

private readonly IOpenLinkCommand _openLinkCommand = new Mock<IOpenLinkCommand>().Object;
private readonly IOpenLinkCommand _openLinkCommand = Substitute.For<IOpenLinkCommand>();

[Fact]
public void GettingFullSize__ReturnsFullSizeFromLocations()
Expand Down
43 changes: 22 additions & 21 deletions Twiddler.Tests/Screens/LoadingTweetScreenTests.cs
Expand Up @@ -2,7 +2,9 @@
{
#region Using Directives

using Moq;
using NSubstitute;

using Should.Fluent;

using Twiddler.Core.Models;
using Twiddler.Core.Services;
Expand All @@ -16,26 +18,25 @@

public class LoadingTweetScreenTests
{
private readonly Mock<ITweetStore> _fakeStore = new Mock<ITweetStore>();

private readonly Mock<ITweetPlaceholderScreen> _fakeTweetPlaceholderScreen = new Mock<ITweetPlaceholderScreen>();
private readonly ITweetStore _store = Substitute.For<ITweetStore>();

private readonly ITweet _tweet = A.Tweet.Build();

private Mock<ITweetScreen> _fakeTweetScreen;
private readonly ITweetPlaceholderScreen _tweetPlaceholderScreen = Substitute.For<ITweetPlaceholderScreen>();

private bool _storeAskedForTweet;

private ITweetScreen _tweetScreen;

public LoadingTweetScreenTests()
{
_fakeTweetPlaceholderScreen.
Setup(x => x.CanShutdown()).
Returns(true);

_fakeStore.
Setup(x => x.GetTweet(_tweet.Id)).
Callback(() => _storeAskedForTweet = true).
Returns(_tweet);
_tweetPlaceholderScreen.CanShutdown().Returns(true);

_store.GetTweet(_tweet.Id).Returns(_ =>
{
_storeAskedForTweet = true;
return _tweet;
});
}

[Fact]
Expand All @@ -53,8 +54,8 @@ public void Initialize_WhenStoreReturnsTweet_OpensTweetScreen()

InitializeAndWaitUntilStoreIsAskedForTweet(test);

_fakeTweetScreen.Verify(x => x.Initialize());
Assert.Same(_fakeTweetScreen.Object, test.ActiveScreen);
_tweetScreen.Received().Initialize();
test.ActiveScreen.Should().Be.SameAs(_tweetScreen);
}

[Fact]
Expand All @@ -64,7 +65,7 @@ public void Initialize__OpensPlaceholderScreen()

test.Initialize();

Assert.Same(_fakeTweetPlaceholderScreen.Object, test.ActiveScreen);
test.ActiveScreen.Should().Be.SameAs(_tweetPlaceholderScreen);
}

[Fact]
Expand All @@ -74,16 +75,16 @@ public void Initialize__RequestsTweet()

InitializeAndWaitUntilStoreIsAskedForTweet(test);

_fakeStore.Verify(x => x.GetTweet(_tweet.Id));
_store.Received().GetTweet(_tweet.Id);
}

private LoadingTweetScreen BuildDefaultTestSubject()
{
_fakeTweetScreen = new Mock<ITweetScreen>();
return new LoadingTweetScreen(_fakeTweetPlaceholderScreen.Object,
_fakeStore.Object,
_tweetScreen = Substitute.For<ITweetScreen>();
return new LoadingTweetScreen(_tweetPlaceholderScreen,
_store,
_tweet.Id,
x => _fakeTweetScreen.Object);
x => _tweetScreen);
}

private void InitializeAndWaitUntilStoreIsAskedForTweet(LoadingTweetScreen test)
Expand Down
33 changes: 14 additions & 19 deletions Twiddler.Tests/Screens/RequestMeterScreenTests.cs
Expand Up @@ -7,7 +7,7 @@

using Caliburn.Testability.Extensions;

using Moq;
using NSubstitute;

using Twiddler.Screens;
using Twiddler.Services.Interfaces;
Expand All @@ -21,15 +21,14 @@ public class RequestMeterScreenTests
{
private static readonly DateTime EndOfPeriod = new DateTime(2000, 1, 1);

private readonly Mock<IClock> _fakeClock = new Mock<IClock>();
private readonly IClock _clock = Substitute.For<IClock>();

private readonly Mock<IRequestLimitStatus> _fakeRequestStatus = new Mock<IRequestLimitStatus>();
private readonly IRequestLimitStatus _requestStatus = Substitute.For<IRequestLimitStatus>();

public RequestMeterScreenTests()
{
_fakeRequestStatus.SetupAllProperties();
_fakeRequestStatus.Object.PeriodEndTime = EndOfPeriod;
_fakeRequestStatus.Setup(x => x.PeriodDuration).Returns(TimeSpan.FromMinutes(100));
_requestStatus.PeriodEndTime = EndOfPeriod;
_requestStatus.PeriodDuration.Returns(TimeSpan.FromMinutes(100));
}

[Fact]
Expand All @@ -38,7 +37,7 @@ public void GettingHourlyLimit__GetsHourlyLimitFromLimitStatus()
RequestMeterScreen test = BuildDefaultTestSubject();

const int hourlyLimit = 350;
_fakeRequestStatus.Object.HourlyLimit = hourlyLimit;
_requestStatus.HourlyLimit = hourlyLimit;

Assert.Equal(hourlyLimit, test.HourlyLimit);
}
Expand All @@ -47,9 +46,7 @@ public void GettingHourlyLimit__GetsHourlyLimitFromLimitStatus()
public void GettingPeriodDuration__GetsFormattedPeriodDurationFromLimitStatus()
{
var duration = new TimeSpan(1, 23, 0);
_fakeRequestStatus.
Setup(x => x.PeriodDuration).
Returns(duration);
_requestStatus.PeriodDuration.Returns(duration);

RequestMeterScreen test = BuildDefaultTestSubject();
test.Initialize();
Expand All @@ -63,7 +60,7 @@ public void GettingRemainingHits__GetsRemainingHitsFromLimitStatus()
RequestMeterScreen test = BuildDefaultTestSubject();

const int remainingHits = 33;
_fakeRequestStatus.Object.RemainingHits = remainingHits;
_requestStatus.RemainingHits = remainingHits;

Assert.Equal(remainingHits, test.RemainingHits);
}
Expand Down Expand Up @@ -101,8 +98,8 @@ public void GettingUsedHitsFraction__ReturnsFractionOfHourlyLimit(int remainingH
{
RequestMeterScreen test = BuildDefaultTestSubject();

_fakeRequestStatus.Object.RemainingHits = remainingHits;
_fakeRequestStatus.Object.HourlyLimit = 100;
_requestStatus.RemainingHits = remainingHits;
_requestStatus.HourlyLimit = 100;

Assert.Equal(fraction, test.UsedHitsFraction);
}
Expand Down Expand Up @@ -160,20 +157,18 @@ public void Shutdown__UnsubscribesFromRequestStatusChanges()

private RequestMeterScreen BuildDefaultTestSubject()
{
return new RequestMeterScreen(_fakeRequestStatus.Object, _fakeClock.Object);
return new RequestMeterScreen(_requestStatus, _clock);
}

private void PropertyChangesOnRequestStatus(string propertyName)
{
_fakeRequestStatus.Raise(x => x.PropertyChanged += null,
new PropertyChangedEventArgs(propertyName));
_requestStatus.PropertyChanged +=
Raise.Event<PropertyChangedEventHandler>(new PropertyChangedEventArgs(propertyName));
}

private void TimeLeftInPeriodIs(TimeSpan remainingTime)
{
_fakeClock.
Setup(x => x.Now).
Returns(EndOfPeriod - remainingTime);
_clock.Now.Returns(EndOfPeriod - remainingTime);
}
}
}

0 comments on commit 6527ef8

Please sign in to comment.