ich eine Klasse haben, so etwas wie folgt aus: -Mocking eine Singleton C# -Klasse in nunit
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 ist weiter eine versiegelte Klasse.
namespace CalendarIntegration.Google
{
public sealed class GoogleWatchManager
{
readonly bool isGoogleTwoWaySyncLive = true;
public GoogleWatchManager()
{
isGoogleTwoWaySyncLive = false;
}
virtual public bool IsGoogleTwoWaySynLive
{
get { return isGoogleTwoWaySyncLive; }
}
Ich möchte gefälschte/Mock GoogleWatchManager Klasse und den Wert von GoogleWatchManager.Instance.IsGoogleTwoWaySynLive
in den nunit Testfälle machen, die standardmäßig in GoogleWatchManager Klasse wahr ist.
habe ich versucht, die unten, aber es nicht
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
}
}
}
Jede Hilfe, Dank im Voraus wird dringend nicht arbeits geschätzt.
Sind Sie die Kontrolle über die Klassen im Beispiel? Wenn dies der Fall ist, müssen Sie hinter einer von Ihnen kontrollierten Abstraktion kapseln und dann in die abhängige Klasse spotten und injizieren. Klassen sollten auch von Abstraktionen und nicht von Konkretionen oder Implementierungsbelangen abhängig sein. – Nkosi
@Nkosi Hast du ein Beispiel? Ich bin ein Neuling in .net. –