示例#1
0
        public void Setup()
        {
            var buildFolder     = new NPath(GetType().Assembly.Location).Parent;
            var systemUnderTest = buildFolder.Combine("SystemUnderTest.dll"); // do not access type directly, want to avoid loading the assembly until it's patched

            m_Dispose = ElevatedSubstitutionContext.AutoHook(systemUnderTest);
        }
        public void BasicTest()
        {
            var assemblyPath        = Assembly.GetExecutingAssembly().Location.ToNPath().Parent;
            var systemUnderTestPath = assemblyPath.Combine("Minimal.Systems.dll");
            var testPath            = assemblyPath.Combine("Minimal.Tests.dll");

            // TODO: use testAssembly to scan for uses of Substitute.For and retrieve System.DateTime.Now and Class.Add

            using (var systemUnderTest = AssemblyDefinition.ReadAssembly(systemUnderTestPath))
                using (var mscorlib = AssemblyDefinition.ReadAssembly(typeof(object).Assembly.Location))
                {
                    // list of all stuff we detect an NSub extension called on it
                    var mockedMethodDefinitions = new[]
                    {
                        mscorlib.MainModule.GetType("System.DateTime").Properties.Single(p => p.Name == "Now").GetMethod,
                        systemUnderTest.MainModule.GetType("Inner").Methods.Single(m => m.Name == "Add"),
                    };

                    Patch(mockedMethodDefinitions);

                    Write(systemUnderTest, systemUnderTestPath);
                    Write(mscorlib, BaseDir.Combine("mscorlib.dll"));
                }

            // would like to do this in an appdomain but complicated
            using (ElevatedSubstitutionContext.AutoHook())
            {
                var testAssembly = Assembly.LoadFile(testPath);
                var fixtureType  = testAssembly.GetType("Fixture", true);
                var fixture      = Activator.CreateInstance(fixtureType);
                var mockMethod   = fixtureType.GetMethod("Mock");
                mockMethod.Invoke(fixture, Array.Empty <object>());
            }
        }
示例#3
0
    public void Mock()
    {
        using (ElevatedSubstitutionContext.AutoHook())
        //using (SubstituteStatic.For<DateTime>())
        {
            var today = DateTime.Today;
            //DateTime.Now.Returns(today);

            var c = Substitute.For <Inner>();
            c.Add(1, 2).Returns(5);

            //Assert.Equals(today, DateTime.Now);
            Assert.Equals(3, c.Add(2, 1));
            Assert.Equals(/*5*/ 6, c.Add(1, 2)); // failing on purpose
        }
    }
 public void Setup()
 {
     m_Dispose = ElevatedSubstitutionContext.AutoHook();
 }
 public void Setup()
 {
     m_Dispose = ElevatedSubstitutionContext.AutoHook();
     PatchedAssemblyBridgeX.TryMock = PatchedAssemblyBridge.TryMock;
 }