public void InheritedMethodAttribute_ExecutesTypesInSetup()
        {
            // Arrange

            // Create dynamic type
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run);
            var moduleBuilder   = assemblyBuilder.DefineDynamicModule("Module");
            var typeBuilder     = moduleBuilder.DefineType("MyNamespace.Base", TypeAttributes.Public | TypeAttributes.Abstract);

            typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

            var abstractMethod = typeBuilder.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);

            var con     = typeof(DependantAttribute).GetConstructor(new Type[] { typeof(Type[]) });
            var builder = new CustomAttributeBuilder(con, new object[] { new Type[] { typeof(FaultyPingMock) } });

            abstractMethod.SetCustomAttribute(builder);

            // Create the type itself
            var baseType = typeBuilder.CreateType();

            // Create another dynamic type
            typeBuilder = moduleBuilder.DefineType("MyNamespace.Child", TypeAttributes.Public, baseType);
            typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

            var newMethod = typeBuilder.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.ReuseSlot | MethodAttributes.Virtual | MethodAttributes.HideBySig);

            newMethod.SetCustomAttribute(builder);

            var il = newMethod.GetILGenerator();

            il.Emit(OpCodes.Ret);

            var newType = typeBuilder.CreateType();

            var attribute = new DependantAttribute();
            var mock      = new MethodInfoMock(newType.GetMethods()[0]);

            // Act
            var results = attribute.BuildFrom(mock, new TestSuite("Test"));

            Assert.IsNotEmpty(results);
            foreach (var result in results)
            {
                // Assert
                Assert.AreEqual(RunState.Ignored, result.RunState);
            }
        }
Exemple #2
0
        public void InvalidTypeSpecified_CreatesInvalidTestMethod(params Type[] types)
        {
            // Arrange
            var testType  = new TypeInfoMock(typeof(ExecuteInSetupTests));
            var attribute = new DependantAttribute(types);
            var testMock  = attribute.BuildFrom(testType).First();
            var mock      = new MethodInfoMock(typeof(ExecuteInSetupTests).GetMethod(nameof(InvalidTypeSpecified_CreatesInvalidTestMethod)));

            // Act
            var results = attribute.BuildFrom(mock, testMock);

            Assert.IsNotEmpty(results);
            foreach (var result in results)
            {
                // Assert
                Assert.AreEqual(RunState.NotRunnable, result.RunState);
            }
        }