Question

I'm setting up unit tests for my project (Project A) in a separate project (Project B) within the same solution file. My issue is that I'm not able to access any methods in all of my repositories for testing. I've already set a reference for Project A's repositories in Project B and I'm able to instantiate any repositories in Project B. I'm just not able to access any of their methods provided by their interfaces.

Here's an example for my class, interface, and repository for one entity. All other entities follow the same architectural pattern.

Class:

public class MyConfiguration
{
    public virtual int Id { get; protected set; }
    public virtual string DefaultLocation { get; set; }
    public virtual string FolderLocalPath { get; set; }
    public virtual string DefaultPrinter { get; set; }
    public virtual int DefaultPrinterLabelAmount { get; set; }
    public virtual string DefaultCompanyName { get; set; }
    public virtual bool CompanyNameEnabled { get; set; }
    public virtual bool TimestampLabelEnabled { get; set; }
    public virtual bool CollectionFeeEnabled { get; set; }
}

Interface:

interface IMyConfigurationRepository
{
    void CreateConfig(MyConfiguration config);
    void UpdateConfig(MyConfiguration config);
    MyConfiguration GetConfig();
}

Repository:

public class MyConfigurationRepository : IMyConfigurationRepository
{
    void IMyConfigurationRepository.CreateConfig(MyConfiguration config)
    {
        throw new NotImplementedException();
    }

    void IMyConfigurationRepository.UpdateConfig(MyConfiguration config)
    {
        throw new NotImplementedException();
    }

    MyConfiguration IMyConfigurationRepository.GetConfig()
    {
        throw new NotImplementedException();
    }
}

I guess my question is, am I doing the interface implementation right?

Was it helpful?

Solution

You are using the 'explicit' form of implementation.

That means you can indeed not call the methods on an instance of the Repsoitory. You need to cast it to the interface:

var repo = new MyConfigurationRepository ();
var gate = (IMyConfigurationRepository) repo;

repo.CreateConfig(...);   // won't compile        
gate.CreateConfig(...);   // OK

It depends on ther rest of your architecture if this what you want. The alternative is to implement implicitily:

public class MyConfigurationRepository : IMyConfigurationRepository
{
    //void IMyConfigurationRepository.CreateConfig(MyConfiguration config)
    public void CreateConfig(MyConfiguration config)
    {
        throw new NotImplementedException();
    }
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top