在 nunit 中模拟单例 c# class
Mocking a singleton c# class in nunit
我有一个 class,像这样:-
namespace CalendarIntegration.Google
{
public sealed class GoogleSyncEventProcessor : ICalendarSyncEventProcessor
{
public void ProcessEventRequest(object events, int soUserId, string calendarId, bool addLogs = false)
{
if (GoogleWatchManager.Instance.IsGoogleTwoWaySynLive)
{
GoogleWatchManager 是进一步密封的class。
namespace CalendarIntegration.Google
{
public sealed class GoogleWatchManager
{
readonly bool isGoogleTwoWaySyncLive = true;
public GoogleWatchManager()
{
isGoogleTwoWaySyncLive = false;
}
virtual public bool IsGoogleTwoWaySynLive
{
get { return isGoogleTwoWaySyncLive; }
}
我想要 fake/mock GoogleWatchManager class 并在 nunit 测试用例中设置 GoogleWatchManager.Instance.IsGoogleTwoWaySynLive
的值,默认情况下它在 GoogleWatchManager class.
我尝试了以下方法,但它不起作用-
using EFBL;
using NUnit.Framework;
using EFBL.CalendarIntegration.CalendarSync;
using EFBL.CalendarIntegration.Google;
using Moq;
namespace EFBL.CalendarIntegration.Google
{
[TestFixture]
public class GoogleSyncEventProcessorSpec
{
public GoogleSyncEventProcessor google;
public GoogleWatchManager googleManager;
public void SetUp()
{
}
[Test]
public void ProcessEventRequest_NoEvents_ExceptionThrown()
{
var mock = new Mock<GoogleWatchManager>();
mock.SetupGet(foo => foo.IsGoogleTwoWaySynLive).Returns(true);
// watch.Setup(i => i.IsGoogleTwoWaySynLive).Returns(false);
// var mock = new Mock<GoogleWatchManager>().Object;
GoogleSyncEventProcessor obj = GoogleSyncEventProcessor.Instance;
obj.ProcessEventRequest(null, -1, "");
// isGoogleTwoWaySyncLive
}
}
}
非常感谢任何帮助,提前致谢。
解决方法如下:-
using System;
using EFBL;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;
using System.Diagnostics;
namespace EFBL.CalendarIntegration.Google
{
[TestFixture]
public class GoogleSyncEventProcessorSpec
{
public GoogleSyncEventProcessor googleSync;
public GoogleWatchManager googleManager;
[SetUp]
public void Init() {
googleManager = Isolate.Fake.Instance<GoogleWatchManager>();
googleSync = GoogleSyncEventProcessor.Instance;
}
[Test]
public void RequiresThatTwoWaySyncLiveBeFalse()
{
Isolate.NonPublic.Property.WhenGetCalled(googleManager, "IsGoogleTwoWaySynLive").WillReturn(false);
Assert.AreEqual(false, googleManager.IsGoogleTwoWaySynLive);
}
我有一个 class,像这样:-
namespace CalendarIntegration.Google
{
public sealed class GoogleSyncEventProcessor : ICalendarSyncEventProcessor
{
public void ProcessEventRequest(object events, int soUserId, string calendarId, bool addLogs = false)
{
if (GoogleWatchManager.Instance.IsGoogleTwoWaySynLive)
{
GoogleWatchManager 是进一步密封的class。
namespace CalendarIntegration.Google
{
public sealed class GoogleWatchManager
{
readonly bool isGoogleTwoWaySyncLive = true;
public GoogleWatchManager()
{
isGoogleTwoWaySyncLive = false;
}
virtual public bool IsGoogleTwoWaySynLive
{
get { return isGoogleTwoWaySyncLive; }
}
我想要 fake/mock GoogleWatchManager class 并在 nunit 测试用例中设置 GoogleWatchManager.Instance.IsGoogleTwoWaySynLive
的值,默认情况下它在 GoogleWatchManager class.
我尝试了以下方法,但它不起作用-
using EFBL;
using NUnit.Framework;
using EFBL.CalendarIntegration.CalendarSync;
using EFBL.CalendarIntegration.Google;
using Moq;
namespace EFBL.CalendarIntegration.Google
{
[TestFixture]
public class GoogleSyncEventProcessorSpec
{
public GoogleSyncEventProcessor google;
public GoogleWatchManager googleManager;
public void SetUp()
{
}
[Test]
public void ProcessEventRequest_NoEvents_ExceptionThrown()
{
var mock = new Mock<GoogleWatchManager>();
mock.SetupGet(foo => foo.IsGoogleTwoWaySynLive).Returns(true);
// watch.Setup(i => i.IsGoogleTwoWaySynLive).Returns(false);
// var mock = new Mock<GoogleWatchManager>().Object;
GoogleSyncEventProcessor obj = GoogleSyncEventProcessor.Instance;
obj.ProcessEventRequest(null, -1, "");
// isGoogleTwoWaySyncLive
}
}
}
非常感谢任何帮助,提前致谢。
解决方法如下:-
using System;
using EFBL;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;
using System.Diagnostics;
namespace EFBL.CalendarIntegration.Google
{
[TestFixture]
public class GoogleSyncEventProcessorSpec
{
public GoogleSyncEventProcessor googleSync;
public GoogleWatchManager googleManager;
[SetUp]
public void Init() {
googleManager = Isolate.Fake.Instance<GoogleWatchManager>();
googleSync = GoogleSyncEventProcessor.Instance;
}
[Test]
public void RequiresThatTwoWaySyncLiveBeFalse()
{
Isolate.NonPublic.Property.WhenGetCalled(googleManager, "IsGoogleTwoWaySynLive").WillReturn(false);
Assert.AreEqual(false, googleManager.IsGoogleTwoWaySynLive);
}