public void TestReplacePropertySetterAction() { var getterExecuted = false; var getterShim = Shim.Replace(() => Is.A <Thread>().CurrentCulture) .With((Thread t) => { getterExecuted = true; return(t.CurrentCulture); }); var setterExecuted = false; var setterShim = Shim.Replace(() => Is.A <Thread>().CurrentCulture, true) .With((Thread t, CultureInfo value) => { setterExecuted = true; t.CurrentCulture = value; }); var currentCultureProperty = typeof(Thread).GetProperty(nameof(Thread.CurrentCulture), typeof(CultureInfo)); Assert.AreEqual(currentCultureProperty.GetMethod, getterShim.Original); Assert.AreEqual(currentCultureProperty.SetMethod, setterShim.Original); PoseContext.Isolate(() => { var oldCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = oldCulture; }, getterShim, setterShim); Assert.IsTrue(getterExecuted, "Getter not executed"); Assert.IsTrue(setterExecuted, "Setter not executed"); }
public void Test1() { Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4)); PoseContext.Isolate(() => { Assert.Equal(new DateTime(2004, 4, 4), DateTime.Now); }, dateTimeShim); var myClass = new MyClassAuto(null, null); var myClassShim = Shim.Replace(() => myClass.GetGreetingDate(Is.A <string>())) //.With<MyClassAuto, DateTime>(x => new DateTime(2021, 01, 01)); .With(delegate(MyClassAuto c) { return(new DateTime(2021, 01, 01)); }); DateTime actualResult = new DateTime(); PoseContext.Isolate(() => { actualResult = myClass.GetGreetingDate(null); }, myClassShim); Assert.Equal(new DateTime(2021, 01, 01), actualResult); }
public void Throws_WhenCantResolve_DependencyLibraries() { var manager = new DefaultMetadataReferenceManager(); IReadOnlyList <CompilationLibrary> a = new List <CompilationLibrary>(); Shim classPropShim = Shim.Replace(() => Is.A <DependencyContext>().CompileLibraries).With((DependencyContext @this) => a); Exception ex = null; try { PoseContext.Isolate(() => { manager.Resolve(DependencyContext.Default); }, classPropShim); } catch (Exception e) { ex = e.InnerException; } Assert.NotNull(ex); Assert.NotNull(ex.Message); Assert.Equal(ex.Message, "Can't load metadata reference from the entry assembly. " + "Make sure PreserveCompilationContext is set to true in *.csproj file"); }
public void TestReplacePropertySetter() { Shim shim = Shim.Replace(() => Is.A <Thread>().CurrentCulture, true); Assert.AreEqual(typeof(Thread).GetProperty(nameof(Thread.CurrentCulture), typeof(CultureInfo)).SetMethod, shim.Original); Assert.IsNull(shim.Replacement); }
public void ShimExample() { Shim classShim = Shim.Replace(() => Is.A <MyClass>().DoSomething()).With( delegate(MyClass @this) { Console.WriteLine("doing someting else"); }); Shim consoleShim = Shim.Replace(() => Console.WriteLine(Is.A <string>())).With( delegate(string s) { Console.WriteLine("Hijacked: {0}", s); }); // This block executes immediately PoseContext.Isolate(() => { // All code that executes within this block // is isolated and shimmed methods are replaced // Outputs "Hijacked: Hello World!" Console.WriteLine("Hello World!"); //// Outputs "4/4/04 12:00:00 AM" //Console.WriteLine(DateTime.Now); //// Outputs "doing someting else" new MyClass().DoSomething(); //// Outputs "doing someting else with myClass" //myClass.DoSomething(); }, consoleShim, classShim); //, dateTimeShim, classPropShim, classShim, myClassShim, structShim); }
internal static Shim ShimGetByteArrayAsync(byte[] buffer) { return(Shim.Replace(() => Is.A <HttpClient>().GetByteArrayAsync(Is.A <Uri>())) .With((HttpClient @this, Uri uri) => { return new Task <byte[]>(() => buffer); })); }
public void TestShimReplaceWithInvalidSignature() { ShimTests shimTests = new ShimTests(); Shim shim = Shim.Replace(() => shimTests.TestReplace()); Assert.ThrowsException <InvalidShimSignatureException>( () => Shim.Replace(() => shimTests.TestReplace()).With(() => { })); Assert.ThrowsException <InvalidShimSignatureException>( () => Shim.Replace(() => Console.WriteLine(Is.A <string>())).With(() => { })); }
internal static Shim ShimFileCreate() { return(Shim.Replace(() => File.Create(Is.A <string>())) .With((string s) => { return new FileStream(@"C:\Users\Iliyan\Desktop\temp", FileMode.Create, FileAccess.Write); })); }
internal static TResponseReference ShimGetAsync <TRequest, TResponseReference>() where TRequest : Request { (HttpResponseMessage response, TResponseReference expected) = MockHttpResponse <TRequest, TResponseReference>(); Shim.Replace(() => Is.A <HttpClient>().GetAsync(Is.A <Uri>())) .With((HttpClient @this, Uri uri) => { return(new Task <HttpResponseMessage>(() => response)); }); return(expected); }
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 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 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); }
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); }
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 BasicTest() { var test = string.Empty; var testShimmy = Shimmy.CreateShimmy <ShimMe>(typeof(ShimMe), new ShimmySettings()); // var iEnumerableShimmy = Shimmy.CreateShimmy<IEnumerable<string>>(typeof(IEnumerable<string>)); // iEnumerableShimmy.GetShim(nameof(IEnumerable<string>.GetEnumerator)).With() var realShim = testShimmy.GetShim(nameof(ShimMe.PublicInstanceParameters), false, typeof(string), typeof(string)); realShim.With((ShimMe @this, string s) => "Wow, getting Shims worked right out of the box!"); var newFrameworkShim = Shim.Replace(() => Is.A <IEnumerable <string> >().FirstOrDefault(Is.A <Func <string, bool> >())) .With((IEnumerable <string> @this, Func <string, bool> func) => @this.FirstOrDefault(func)); //var otherFrameworkShim = Shim.Replace(() => string.Format(Is.A<string>(), Is.A<object[]>())) // .With((string replacement, object[] args) => string.Format(replacement, args)); //var otherOtherFrameworkShim = Shim.Replace(() => string.Format(Is.A<string>(), Is.A<object>())) // .With((string replacement, object args) => string.Format(replacement, args)); var otherOtherOtherFrameworkShim = Shim.Replace(() => string.Format(Is.A <string>(), Is.A <object>(), Is.A <object>()), false) .With((string replacement, object args, object otherArg) => { return(string.Format(replacement, args, otherArg)); }); var moreFrameworkShim = Shim.Replace(() => string.Format(Is.A <string>(), Is.A <object>()), false) .With((string replacement, object arg) => { return(string.Format(replacement, arg)); }); // var otherRealShim = testShimmy.GetShim("get_PublicProperty"); Poise.Poise.Run(() => { test = DontShimMe.TestMeThough(); }, new List <Shimmy> { testShimmy }, new List <Shim>() { newFrameworkShim, otherOtherOtherFrameworkShim, moreFrameworkShim }); //otherOtherOtherFrameworkShim });//, iEnumerableShimmy }); Debug.WriteLine(test); }
public void PoseMethod() { var replaceMethod = Shim.Replace(() => Is.A <PoseSample>().PrintMsg()) .With(delegate(PoseSample @this) { Console.WriteLine("122"); }); var replaceReturnInt = Shim.Replace(() => Is.A <PoseSample>().ReturnAgeValue()) .With(delegate(PoseSample @this) { throw new IOException(); return(100); }); PoseContext.Isolate(() => { var poseSample = new PoseSample(); poseSample.PrintMsg(); var age = poseSample.ReturnAgeValue(); Console.WriteLine(age); }, replaceMethod, replaceReturnInt); }
public CoreLoggerExtensionsTests() { _logShim = Shim.Replace(() => ApplicationLogger.Log( Is.A <object>(), Is.A <LogLevel>(), Is.A <EventId>(), Is.A <Exception>(), Is.A <string>(), Is.A <object[]>())) .With(delegate ( object loggingCategory, LogLevel logLevel, EventId eventId, Exception exception, string message, object[] args ) { LoggerExtensions.Log(_testLogger, logLevel, eventId, exception, message, args); }); }