Beispiel #1
0
        /// <summary>
        /// Call if your test needs to access classes via MEF.  Loads all dlls in the test directory.
        ///
        /// <para>This must be called before you 'launch' your ui</para>
        /// </summary>
        protected void SetupMEF()
        {
            MEF = new MEF();
            MEF.Setup(new SafeDirectoryCatalog(new IgnoreAllErrorsCheckNotifier(), TestContext.CurrentContext.TestDirectory));
            Repository.CatalogueRepository.MEF = MEF;

            Validator.RefreshExtraTypes(MEF.SafeDirectoryCatalog, new ThrowImmediatelyCheckNotifier());
        }
Beispiel #2
0
        public void TestAllSupported()
        {
            //load all DatabaseEntity types
            MEF mef = new MEF();

            mef.Setup(new SafeDirectoryCatalog(TestContext.CurrentContext.TestDirectory));

            var types = mef.GetAllTypes()
                        .Where(t => typeof(DatabaseEntity).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).ToArray();

            var methods = typeof(UnitTests).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            var method  = methods.Single(m => m.Name.Equals("WhenIHaveA") && !m.GetParameters().Any());

            List <Type> notSupported = new List <Type>();

            foreach (Type t in types)
            {
                //ignore these types too
                if (SkipTheseTypes.Contains(t.Name) || t.Name.StartsWith("Spontaneous") || typeof(SpontaneousObject).IsAssignableFrom(t))
                {
                    continue;
                }

                DatabaseEntity instance = null;

                try
                {
                    //ensure that the method supports the Type
                    var generic = method.MakeGenericMethod(t);
                    instance = (DatabaseEntity)generic.Invoke(this, null);
                }
                catch (TargetInvocationException exception)
                {
                    if (exception.InnerException is TestCaseNotWrittenYetException)
                    {
                        notSupported.Add(t);
                    }
                    else
                    {
                        throw;
                    }
                }

                //if the instance returned by MakeGenericMethod does not pass checks that's a dealbreaker!
                if (instance != null)
                {
                    try
                    {
                        //and that it returns an instance
                        Assert.IsNotNull(instance);
                        Assert.IsTrue(instance.Exists());
                        Assert.AreEqual(ChangeDescription.NoChanges, instance.HasLocalChanges().Evaluation, "Type was '" + t.Name + "'");
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Implementation of WhenIHaveA<" + t.Name + "> is flawed", e);
                    }
                }
            }

            Assert.IsEmpty(notSupported, "The following Types were not supported by WhenIHaveA<T>:" + Environment.NewLine + string.Join(Environment.NewLine, notSupported.Select(t => t.Name)));
        }