Unit testing 在nunit中嘲笑一个单身c#类

Unit testing 在nunit中嘲笑一个单身c#类,unit-testing,mocking,nunit,moq,sealed,Unit Testing,Mocking,Nunit,Moq,Sealed,我有一门课,类似这样:- namespace CalendarIntegration.Google { public sealed class GoogleSyncEventProcessor : ICalendarSyncEventProcessor { public void ProcessEventRequest(object events, int soUserId, string calendarId, bool addLogs = false) {

我有一门课,类似这样:-

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是一个密封的类

namespace CalendarIntegration.Google
{
    public sealed class GoogleWatchManager
    {
        readonly bool isGoogleTwoWaySyncLive = true;
        public GoogleWatchManager()
        {
                   isGoogleTwoWaySyncLive = false;
        }

 virtual public bool IsGoogleTwoWaySynLive
        {
            get { return isGoogleTwoWaySyncLive; }
        }
我想要假/假GoogleWatchManager类,并在nunit测试用例中设置
GoogleWatchManager.Instance.IsGoogleTwoWaySynLive
的值,默认情况下,在GoogleWatchManager类中为true

我试过下面的方法,但不起作用-

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
        }
    }
}
使用EFBL;
使用NUnit.Framework;
使用EFBL.CalendarIntegration.CalendarSync;
使用EFBL.CalendarIntegration.Google;
使用最小起订量;
名称空间EFBL.CalendarIntegration.Google
{
[测试夹具]
公共类GoogleSyncEventProcessorSpec
{
公共谷歌;
公众谷歌观察经理;
公共作废设置()
{
}
[测试]
public void ProcessEventRequest\u NoEvents\u exception rown()
{
var mock=new mock();
mock.SetupGet(foo=>foo.IsGoogleTwoWaySynLive).Returns(true);
//watch.Setup(i=>i.IsGoogleTwoWaySynLive).Returns(false);
//var mock=new mock().Object;
GoogleSyncEventProcessor obj=GoogleSyncEventProcessor.Instance;
obj.ProcessEventRequest(null,-1,“”);
//谷歌双向同步直播吗
}
}
}
非常感谢您的帮助,请提前感谢。

以下是解决方案:-

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);
        }
使用系统;
使用EFBL;
使用NUnit.Framework;
使用TypeMock.ArrangeActAssert;
使用系统诊断;
名称空间EFBL.CalendarIntegration.Google
{
[测试夹具]
公共类GoogleSyncEventProcessorSpec
{
公共GoogleSyncEventProcessor googleSync;
公众谷歌观察经理;
[设置]
公共void Init(){
googleManager=Isolate.false.Instance();
googleSync=GoogleSyncEventProcessor.Instance;
}
[测试]
public void要求TwoWaySyncLiveBeFalse()
{
隔离.NonPublic.Property.WhenGetCalled(googleManager,“IsGoogleTwoWaySynLive”)。将返回(false);
AreEqual(false,googleManager.isgoogletwaysynlive);
}

您是否控制了示例中的类?如果是这样,那么您需要封装在您控制的抽象之后,然后模拟并注入到依赖类中。类还应该依赖于抽象,而不是具体化或实现问题。@Nkosi您有任何示例吗?我是.net的新手。