views:

268

answers:

1

Hi,

How can I Mock ConfigurationCollection with Rhino Mocks.

I need to create an Expected ConfigurationCollection which contains 1 element in order to test if me Configuration contains that element.

Thanks.

Note : My ConfigurationCollection inherits from IEnumerable.

public interface ICapalConfiguration
{
    ICapalRepositoryConfigurationCollection Repositories { get; }
    ICapalServiceConfigurationCollection Services { get; }
}

public interface ICapalRepositoryConfigurationCollection : IEnumerable
{
    string DefaultConnectionString { get; set; }
    ICapalRepositoryConfiguration GetElement(string name);
}

public interface ICapalRepositoryConfiguration
{
    string Name { get; set; }
    string ConnectionStringName { get; set; }
}


    [TestMethod]
    public void Can_Get_One_Repository()
    {
        var config = MockRepository.GenerateMock<ICapalConfiguration>();
        var expected_repositories = MockRepository.GenerateMock<ICapalRepositoryConfigurationCollection>();
        var expected_repository = MockRepository.GenerateMock<ICapalRepositoryConfiguration>();

        config.Expect(p => p.Repositories).Return(expected_repositories).Repeat.Any();
        config.Expect(p => p.Repositories.GetElement("ArticleRepository")).Return(expected_repository);

        var config_repositories = config.Repositories;
        var config_repository = config.Repositories.GetElement("ArticleRepository");

        config.VerifyAllExpectations();

        Assert.IsNotNull(config);
        Assert.IsNotNull(config_repositories);
    }
+1  A: 

What is your system under test? Looking at your test I think it sould an implementation of ICapalConfiguration therefore you should not mock it.

I also tend to not mock simple data structures such as collections. Just use the real thing.

Simon Laroche
What do you wanna say by : "I also tend to not mock simple data structures such as collections. Just use the real thing."
Yoann. B
Do not mock List, Arrays or Stacks. They are stable and do not depend on external resources. In you case, I think you should just create your collection in your configuration class or inject as a constructor argument. That way you can put mocks of RepositoryConfiguration in your collection.
Simon Laroche