public static void DateTimeRerouting() { var dt = new FDateTime(1, 2, 3); FDateTime.get_NowFake_static = () => { var hook = CastlePatchedInterceptorRegistry.TryGetStaticHook(typeof(FDateTime), 1); if (hook != null) { var results = hook(new object[0]); var retvalue = results[0]; if (!(retvalue is PassthroughPlaceholder)) { return((FDateTime)retvalue); } } return(new FDateTime(DateTime.Now.Ticks)); }; Assert.AreEqual(1, dt.Year); var dtstatic = Substitute.ForStatic <FDateTime>(); FDateTime.Now.Returns(new FDateTime(2, 3, 4)); Assert.AreEqual(2, FDateTime.Now.Year); }
public static void ThreadSleepRerouting() { using (Substitute.ForStatic(typeof(Thread))) { Thread.Sleep(TimeSpan.FromDays(1)); // finishes instantly } }
public static void MultipleStaticsAtOnce() { using (var sdatetime = Substitute.ForStatic <DateTime>()) using (var sconsole = Substitute.ForStatic(typeof(Console))) using (var sthread = Substitute.ForStatic <Thread>()) { } }
public static void DateTimeRerouting() { using (Substitute.ForStatic <DateTime>()) { DateTime.Now.Returns(new DateTime(2010, 1, 1)); DateTime.Now.Year.ShouldBe(2010); } }
public static void ConsoleReadLineRerouting() { using (Substitute.ForStatic(typeof(Console))) { Console.ReadLine().Returns("xyzzy"); Console.ReadLine().ShouldBe("xyzzy"); } }
public static void StaticHooksOk() { using (var cstatic = Substitute.ForStatic <Calculator>()) { Calculator.Square(1.5f).Returns(10f); Assert.That(Calculator.Square(1.5f), Is.EqualTo(10f)); cstatic.Received().Static(() => Calculator.Square(1.5f)); } }
public static void StaticReroutesNotUsedAcrossDisposals() { using (var scalc1 = Substitute.ForStatic <Calculator>()) { Calculator.Square(Arg.Any <float>()).Returns(10); Assert.That(Calculator.Square(3), Is.EqualTo(10)); Calculator.Square(4); scalc1.Received().Static(() => Calculator.Square(4)); scalc1.Received().Static(() => Calculator.Square(Arg.Any <float>())); Assert.That(Calculator.Square(3), Is.EqualTo(10)); } Assert.That(Calculator.Square(3), Is.EqualTo(9), "should be passthru again"); using (var scalc2 = Substitute.ForStatic <Calculator>()) { Assert.That(Calculator.Square(3), Is.EqualTo(0), "should be default again"); scalc2.Received().Static(() => Calculator.Square(3)); } Assert.That(Calculator.Square(4), Is.EqualTo(16), "passthru once more"); }
public static void MissileChecksForCollider() { // ARRANGE using (Substitute.ForStatic <DateTime>()) using (Substitute.ForStatic(typeof(Console))) { var nowTime = new DateTime(1955, 11, 12); var futureTime = new DateTime(1985, 10, 26); DateTime.Now.Returns(nowTime); var collider = Substitute.For <Collider>(); collider.enabled.Returns(true, false, true); var okMissile = new Missile(collider, nowTime); var badMissile = new Missile(collider, nowTime); var notReadyMissile = new Missile(collider, futureTime); Console.ReadLine().Returns("wat"); // ACT Should.NotThrow(() => okMissile.Validate()); Should.NotThrow(() => notReadyMissile.Validate()); Should .Throw <Exception>(() => badMissile.Validate()) .Message.ShouldBe("wat"); // ASSERT okMissile.DidValidate.ShouldBeTrue(); badMissile.DidValidate.ShouldBeFalse(); notReadyMissile.DidValidate.ShouldBeFalse(); } }
public static void DoubleSubstitutionWithDisposeShouldNotThrow() { Substitute.ForStatic <Calculator>().Dispose(); Assert.DoesNotThrow(() => Substitute.ForStatic <Calculator>().Dispose()); }
public static void DoubleSubstitutionWithoutDisposeShouldThrow() { using (Substitute.ForStatic <Calculator>()) Assert.Throws <SubstituteException>(() => Substitute.ForStatic <Calculator>()); }