Search code examples
c#migrationfactorymbunit

Migration from MBUnit v2 to v3 and the ProviderFactory is gone


In MBUnit v2 I did this:

public class ConnectionStringFactory
    {
        [Factory]
        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; 
            }
        }
    }

    [ProviderFactory(typeof(ConnectionStringFactory),typeof(string))]
    public class CustomerTests
    {
        public void GetCustomerTest(string connectionString)
        { 

        }

        public void GetCustomersTest(string connectionString)
        {

        }
    }

I had to create ONE Factory class returning me a connectionString which gets injected into each test method of a unit test class.

How can this be done with MBUnit v3 where the ProviderFactory is gone?

I played a lot with the Factory class, but the result is not what I want.

I want to a Connection string factory used by all test classes where the connection string

is injected into each test method automatically.


Solution

  • How about this?

    public static class ConnectionStringFactory
    {
        public static IEnumerable<string> GetConnectionString()
        {
            yield return "connString";
        }
    }
    
    [Factory(typeof(ConnectionStringFactory), "GetConnectionString")]
    public class CustomerTests
    {
        [Test]
        public void GetCustomerTest(string connectionString)
        {
            Console.WriteLine(connectionString);
        }
    
        [Test]
        public void GetCustomersTest(string connectionString)
        {
            Console.WriteLine(connectionString);
        }
    }