Permalink
Browse files

Replaced Moq with NSubstitute.

  • Loading branch information...
Graeme Foster authored and GraemeF committed Feb 16, 2011
1 parent 0d182c5 commit 6527ef85f36bef51b3db96c94b648148b39fb697
Showing with 550 additions and 16,269 deletions.
  1. +5 −8 Twiddler.Tests/Commands/DeauthorizeCommandTests.cs
  2. +4 −4 Twiddler.Tests/Commands/MarkTweetAsReadCommandTests.cs
  3. +16 −25 Twiddler.Tests/{Screens → }/ReactiveObjectMockHelpers.cs
  4. +2 −2 Twiddler.Tests/Screens/ImageThumbnailScreenTests.cs
  5. +22 −21 Twiddler.Tests/Screens/LoadingTweetScreenTests.cs
  6. +14 −19 Twiddler.Tests/Screens/RequestMeterScreenTests.cs
  7. +10 −10 Twiddler.Tests/Screens/ShellScreenTests.cs
  8. +10 −17 Twiddler.Tests/Screens/StatusScreenTests.cs
  9. +14 −18 Twiddler.Tests/Screens/TimelineScreenTests.cs
  10. +16 −20 Twiddler.Tests/Screens/TweetScreenTests.cs
  11. +2 −2 Twiddler.Tests/Services/LinkThumbnailScreenFactoryTests.cs
  12. +19 −20 Twiddler.Tests/Services/RequestConductorTests.cs
  13. +6 −8 Twiddler.Tests/Services/StoreTimelineTests.cs
  14. +4 −6 Twiddler.Tests/Services/TweetRatingTests.cs
  15. +5 −5 Twiddler.Tests/Services/TwitterStoreUpdaterTests.cs
  16. +3 −3 Twiddler.Tests/Twiddler.Tests.csproj
  17. +1 −1 Twiddler.Tests/packages.config
  18. +11 −11 Twiddler.TweetSharp.Tests/AuthorizeCommandTests.cs
  19. +3 −3 Twiddler.TweetSharp.Tests/Twiddler.TweetSharp.Tests.csproj
  20. +3 −3 Twiddler.TweetSharp.Tests/TwitterClientTests.cs
  21. +1 −1 Twiddler.TweetSharp.Tests/packages.config
  22. +8 −14 Twiddler.TwitterStore.Tests/CredentialsDocumentStoreTests.cs
  23. +2 −2 Twiddler.TwitterStore.Tests/Twiddler.TwitterStore.Tests.csproj
  24. +11 −17 Twiddler.TwitterStore.Tests/TwitterDocumentStoreTests.cs
  25. +1 −1 Twiddler.TwitterStore.Tests/packages.config
  26. +0 −39 packages/Moq.4.0.10827/License.txt
  27. BIN packages/Moq.4.0.10827/Moq.4.0.10827.nupkg
  28. BIN packages/Moq.4.0.10827/Moq.chm
  29. BIN packages/Moq.4.0.10827/lib/NET35/Moq.dll
  30. BIN packages/Moq.4.0.10827/lib/NET35/Moq.pdb
  31. +0 −5,768 packages/Moq.4.0.10827/lib/NET35/Moq.xml
  32. BIN packages/Moq.4.0.10827/lib/NET40/Moq.dll
  33. BIN packages/Moq.4.0.10827/lib/NET40/Moq.pdb
  34. +0 −5,120 packages/Moq.4.0.10827/lib/NET40/Moq.xml
  35. BIN packages/Moq.4.0.10827/lib/Silverlight4/Castle.Core.dll
  36. BIN packages/Moq.4.0.10827/lib/Silverlight4/Moq.Silverlight.dll
  37. BIN packages/Moq.4.0.10827/lib/Silverlight4/Moq.Silverlight.pdb
  38. +0 −5,101 packages/Moq.4.0.10827/lib/Silverlight4/Moq.Silverlight.xml
  39. +35 −0 packages/NSubstitute.1.0.0.0/BreakingChanges.txt
  40. +47 −0 packages/NSubstitute.1.0.0.0/CHANGELOG.txt
  41. +27 −0 packages/NSubstitute.1.0.0.0/LICENSE.txt
  42. BIN packages/NSubstitute.1.0.0.0/NSubstitute.1.0.0.0.nupkg
  43. +113 −0 packages/NSubstitute.1.0.0.0/README.txt
  44. +50 −0 packages/NSubstitute.1.0.0.0/acknowledgements.txt
  45. +85 −0 packages/NSubstitute.1.0.0.0/lib/35/NSubstitute.XML
  46. BIN packages/NSubstitute.1.0.0.0/lib/35/NSubstitute.dll
@@ -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;
@@ -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()
@@ -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);
}
}
}
@@ -2,7 +2,7 @@
{
#region Using Directives
using Moq;
using NSubstitute;
using Twiddler.Commands;
using Twiddler.Core.Models;
@@ -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();
@@ -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
@@ -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)
@@ -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,7 +4,7 @@
using System;
using Moq;
using NSubstitute;
using Twiddler.Commands.Interfaces;
using Twiddler.Models;
@@ -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()
@@ -2,7 +2,9 @@
{
#region Using Directives
using Moq;
using NSubstitute;
using Should.Fluent;
using Twiddler.Core.Models;
using Twiddler.Core.Services;
@@ -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]
@@ -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]
@@ -64,7 +65,7 @@ public void Initialize__OpensPlaceholderScreen()
test.Initialize();
Assert.Same(_fakeTweetPlaceholderScreen.Object, test.ActiveScreen);
test.ActiveScreen.Should().Be.SameAs(_tweetPlaceholderScreen);
}
[Fact]
@@ -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)
@@ -7,7 +7,7 @@
using Caliburn.Testability.Extensions;
using Moq;
using NSubstitute;
using Twiddler.Screens;
using Twiddler.Services.Interfaces;
@@ -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]
@@ -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);
}
@@ -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();
@@ -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);
}
@@ -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);
}
@@ -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);
}
}
}
Oops, something went wrong.

0 comments on commit 6527ef8

Please sign in to comment.