public void TestGetShimDelegateTarget() { Action action = new Action(() => Console.Clear()); Shim shim = Shim.Replace(() => Console.Clear()).With(action); PoseContext.Isolate(() => { }, shim); Assert.AreEqual(action.Target, StubHelper.GetShimDelegateTarget(0)); Assert.AreSame(action.Target, StubHelper.GetShimDelegateTarget(0)); }
public void Execute(bool clearLastCallResults = true, params object[] args) { if (clearLastCallResults) { ClearLastCallResults(); } VerifyArguments(args); PoseContext.IsolateDelegate(EntryPoint, EntryPointType, GetShims(), args); }
public void OpeningHours_IsNowOpen_Pose() { Shim dateShim = Shim.Replace(() => DateTime.Now) .With(() => new DateTime(1848, 3, 22, 12, 22, 50)); DateTime datum = DateTime.Now; // Original PoseContext.Isolate(() => { datum = DateTime.Now; // Fake }, dateShim); }
public void Öffnungszeiten_IsNowOpen_Pose() { Shim dateShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(1000, 1, 1)); DateTime datum = default; PoseContext.Isolate(() => { datum = DateTime.Now; }, dateShim); Assert.AreEqual(new DateTime(1000, 1, 1), datum); }
public void WriteLine_ShouldCallSystemConsoleWriteLineTest2() { // arrange var consoleShim = Shim.Replace(() => System.Console.WriteLine()) .With(() => shimResult.Add("called")); // act PoseContext.Isolate(() => sut.WriteLine(), consoleShim); // assert shimResult.Should().BeEquivalentTo("called"); }
public void WriteLine_ShouldCallSystemConsoleWriteLineTest() { // arrange var consoleShim = Shim.Replace(() => System.Console.WriteLine(Is.A <string>())) .With((string s) => shimResult.Add("shimmed: " + s)); // act PoseContext.Isolate(() => sut.WriteLine("monkey"), consoleShim); // assert shimResult.Should().BeEquivalentTo("shimmed: monkey"); }
public void Shim_static_method() { const string color = "Blue"; Shim fileRead = Shim.Replace(() => File.ReadAllText(Is.A <string>())).With(delegate(string a) { return(color); }); PoseContext.Isolate(() => { var car = new Car(); car.Color.Should().Be(color); }, fileRead); }
// [ExpectedException(typeof(NullReferenceException))] public void ShimConstructor() { Shim ctorShim = Shim.Replace(() => new MyClass()) .With(() => new MyClass { MyProperty = 10 }); PoseContext.Isolate(() => { // this line breaks the Shim Assert.AreEqual(10, 10); }, ctorShim); }
public void OpeningHours_IsNowOpen_Pose() { Shim dateTimeShim = Shim.Replace(() => DateTime.Now) .With(() => new DateTime(1856, 3, 12, 12, 00, 00)); DateTime original = DateTime.Now; PoseContext.Isolate(() => { original = DateTime.Now; // Ab jetzt fake }, dateTimeShim); Assert.AreEqual(new DateTime(1856, 3, 12, 12, 00, 00), original); }
public void IsNowOpen_returns_tonerdo_pose() // OpenSource: Pose { Shim fakeDateTimeNow = Shim.Replace(() => DateTime.Now) .With(() => new DateTime(2199, 12, 24, 13, 55, 00)); DateTime heute = DateTime.Now; // original PoseContext.Isolate(() => { // Logik:..... heute = DateTime.Now; }, fakeDateTimeNow); }
public async Task DownloadPhotoAsync_UrlIdDestination_ReturnsTheSizeOfTheDownloadedFile() { var buffer = new byte[] { 1, 2 }; ClientTestUtilities.ShimGetByteArrayAsync(buffer); ClientTestUtilities.ShimFileCreate(); ClientTestUtilities.ShimGetAsync <DownloadPhotoRequest, PhotoDownloadMarker>(); PoseContext.Isolate(async() => { long fileSize = await client.DownloadPhotoAsync(new Uri("http://a.b.c"), "1", @"C:\1.jpg").ConfigureAwait(false); Assert.Equal(buffer.Length, fileSize); }); }
public static void Run(Action act, IEnumerable <Shimmy> shimmies, IEnumerable <Shim> frameworkShims = null) { var shims = shimmies.SelectMany(x => x.GetShims()).ToList(); //shims.Add(Shim.Replace(() => Is.A<IEnumerable<string>>().FirstOrDefault(Is.A<Func<string, bool>>())).With((IEnumerable<string> @this, Func<string, bool> func) => "MGLTL")); //shims.Add(Shim.Replace(() => IEnumerable<string>.)); //shims.Add(Shim.Replace(() => string.Format(Is.A<string>(), Is.A<object[]>())).With((string s, object[] parms) => string.Format(s, parms))); if (frameworkShims != null) { shims.AddRange(frameworkShims); } PoseContext.Isolate(act, shims.ToArray()); }
public void TestExplicit() { int foo = 0; Shim shim = Shim.Replace(() => Is.A <MyTest>().ExplicitMethod()).With((MyTest t) => 42); var r = new MyTest(); PoseContext.Isolate(() => { foo = r.Explicitly(); }, shim); Assert.AreEqual(42, foo); }
public void PromptPromptYesNoCancel_ReturnsCorrectValue(MessageBoxResult messageBoxResult, MessageResult expectedResult) { Shim shim = Given_MessageBox_Show_Returns(messageBoxResult); MessageResult?actualResult = null; PoseContext.Isolate(() => { actualResult = subject.PromptYesNoCancel("content", "title"); }, shim); Assert.AreEqual(expectedResult, actualResult); }
public void OtherTest() { // Arrange string result = null; // Act PoseContext.Isolate(() => { result = bar.SomeAsyncStuff().Result; }, barShim); // Assert Assert.Equal("lol I'm fake", result); }
public void Pose_On_Tuesday_Should_Be_Yellow() { var dtShim = Pose.Shim.Replace(() => DateTime.Now) .With(() => new DateTime(2017, 1, 31)); PoseContext.Isolate(() => { string expected = "Yellow"; var colors = new FavoriteColorGenerator(); var result = colors.GetFavorite(); Assert.AreEqual(expected, result); }, dtShim); }
public void Test1() { // Arrange int?result = null; // Act PoseContext.Isolate(() => { result = foo.DoSomeIo(); }, fooShim); // Assert Assert.Equal(99, result); }
public void Save_CorrectlySetsSuggestedFileExtension(string input, string expectedResult) { Given_ShimSaveFileDialog_ShowDialog_LogsFileExtensionAndReturnsFalse(); bool?result = null; PoseContext.Isolate(() => { result = subject.Save("content", input, "fileExtensionName", "fileName"); }, shimSaveFileDialog_ShowDialog_Returns); Assert.IsFalse(result); Assert.AreEqual($"File extension: {expectedResult}\r\n", consoleOutput.ToString()); }
public void IsNowOpenTest_Pose() { // NUGet: Pose // Fakekonfiguration Shim dateShim = Shim.Replace(() => DateTime.Now) .With(() => new DateTime(1848, 3, 12, 12, 32, 55)); DateTime date = default; PoseContext.Isolate(() => { // Aktuell (10.10.2019) kann man Breakpoins innerhalb von PoseContext.Isolate nicht nutzen :( date = DateTime.Now; }, dateShim); }
public void WhenConfigFileDoesNotContainAppSetting_DefaultIsUsed() { // Arrange Shim configShim = Shim.Replace(() => ConfigurationManager.AppSettings) .With(() => new NameValueCollection()); PoseContext.Isolate(() => { // Act var actual = ConfigUtil.GetAppSettingWithDefault("SomeKey", "DefaultValue"); // Assert Assert.Equal("DefaultValue", actual); }, configShim); }
public void GreetService_HaveGreatDay() { Shim shim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2019, 2, 20, 20, 50, 0)); var greetService = new GreetService(); var response = string.Empty; PoseContext.Isolate(() => { response = greetService.Greet(); }, shim); Assert.Equal("Have a great day!", response); }
public void ShimConstructor() { Shim ctorShim = Shim.Replace(() => new MyClass()) .With(() => new MyClass { MyProperty = 10 }); PoseContext.Isolate(() => { MyClass myClass = new MyClass(); Assert.AreEqual("10", myClass.MyProperty.ToString()); }, ctorShim); }
public void TestBarFooMethod() { // Uses concrete 'Bar' class but it calls the previously shimmed 'Foo' class. // Arrange int?result = null; // Act PoseContext.Isolate(() => { result = bar.DoSomethingWithFoo(); }, fooShim); // Assert Assert.Equal(99, result); }
public void Save_DoesNotWriteToDiskWhenDialogReturns_False() { Given_ShimSaveFileDialog_ShowDialog_Returns(false); Given_ShimSaveFileDialog_FileName_Gets("fileName"); Given_Shim_File_WriteAllText_DoesNothing(); bool?result = null; PoseContext.Isolate(() => { result = subject.Save("content"); }, shimSaveFileDialog_ShowDialog_Returns, shimSaveFileDialog_FileName_Gets, shim_File_WriteAllText_DoesNothing); Assert.IsFalse(result); Assert.AreEqual("", consoleOutput.ToString()); }
public void GreetService_GoodMorning(string time) { var dateTime = Convert.ToDateTime(time); Shim shim = Shim.Replace(() => DateTime.Now).With(() => dateTime); var greetService = new GreetService(); var response = string.Empty; PoseContext.Isolate(() => { response = greetService.Greet(); }, shim); Assert.Equal("Good morning!", response); }
public void TestLog() { var outputHandler = new OutputHandler(); var consoleLogger = new ConsoleLogger(new DefaultLogFormatter()); Shim consoleLoggerShim = Shim.Replace(() => consoleLogger.Log(Is.A <Guid?>(), Is.A <LogLevel>(), Is.A <string>(), Is.A <IEnumerable <KeyValuePair <string, string> > >())).With( delegate(ConsoleLogger @this, Guid? id, LogLevel logLevel, string message, IEnumerable <KeyValuePair <string, string> > contextParameters) { outputHandler.AddConsoleOutput(new DefaultLogFormatter().GetLogLine(id, logLevel, message, contextParameters)); } ); PoseContext.Isolate(() => { consoleLogger.Log(Guid.NewGuid(), LogLevel.Info, "console-messageText", new Dictionary <string, string>()); Assert.Contains("console-messageText", outputHandler.ConsoleOutput); }, consoleLoggerShim); }
//[TestMethod] public void OpeningHours_IsNowOpen_POSE() { PoseContext.Isolate(() => { Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2011, 11, 11, 1, 11, 11)); //Shim.Replace(() => File.ReadAllText("A")).With(s => "Hallo Welt"); Debug.WriteLine(DateTime.Now); Debug.WriteLine(File.ReadAllText("A")); var oh = new OpeningHours(); var result = oh.IsNowOpen(); Assert.IsTrue(result); //Assert.IsFalse(result); }); }
public void TestGetIndexOfMatchingShim() { StubHelperTests stubHelperTests = new StubHelperTests(); Action staticAction = new Action(() => { }); Action <StubHelperTests> instanceAction = new Action <StubHelperTests>((@this) => { }); Shim shim = Shim.Replace(() => Console.Clear()).With(staticAction); Shim shim1 = Shim.Replace(() => Is.A <StubHelperTests>().TestGetIndexOfMatchingShim()).With(instanceAction); Shim shim2 = Shim.Replace(() => stubHelperTests.TestGetIndexOfMatchingShim()).With(instanceAction); PoseContext.Isolate(() => { }, shim, shim1, shim2); MethodInfo consoleMethodInfo = typeof(Console).GetMethod("Clear"); MethodInfo stubMethodInfo = typeof(StubHelperTests).GetMethod("TestGetIndexOfMatchingShim"); Assert.AreEqual(0, StubHelper.GetIndexOfMatchingShim(consoleMethodInfo, null)); Assert.AreEqual(1, StubHelper.GetIndexOfMatchingShim(stubMethodInfo, new StubHelperTests())); Assert.AreEqual(2, StubHelper.GetIndexOfMatchingShim(stubMethodInfo, stubHelperTests)); }
public void ShimmedContructor_Returns_Null_If_No_Parameterless_Constructor() { var constructorInfo = typeof(TestClassNoParameterlessConstructor).GetConstructor(new [] { typeof(int) }); var shimmedConstructor = new ShimmedConstructor <TestClassNoParameterlessConstructor>(constructorInfo); Assert.IsNotNull(shimmedConstructor); Assert.IsNotNull(shimmedConstructor.Constructor); Assert.IsNotNull(shimmedConstructor.Member); Assert.IsNotNull(shimmedConstructor.Shim); TestClassNoParameterlessConstructor a = null; PoseContext.Isolate(() => { a = new TestClassNoParameterlessConstructor(1); }, shimmedConstructor.Shim); Assert.AreEqual(1, shimmedConstructor.CallResults.Count); Assert.IsNotNull(shimmedConstructor.CallResults.FirstOrDefault()); Assert.IsNull(a); }
public void ShimmedConstructor_Returns_New_Instance_If_Parameterless_Constructor_Available() { var constructorInfo = typeof(InstanceMethodsTestClass).GetConstructor(Type.EmptyTypes); var shimmedConstructor = new ShimmedConstructor <InstanceMethodsTestClass>(constructorInfo); Assert.IsNotNull(shimmedConstructor); Assert.IsNotNull(shimmedConstructor.Constructor); Assert.IsNotNull(shimmedConstructor.Member); Assert.IsNotNull(shimmedConstructor.Shim); InstanceMethodsTestClass a = null; PoseContext.Isolate(() => { a = new InstanceMethodsTestClass(); }, shimmedConstructor.Shim); Assert.AreEqual(1, shimmedConstructor.CallResults.Count); Assert.IsNotNull(shimmedConstructor.CallResults.FirstOrDefault()); Assert.IsNotNull(a); }