Question

I am learning how to use mocks to uncouple some c# code so I can unit test it. I found a great introduction to this on http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B207#fbid=. The problem I am having is that in one of the classes has a struct. How would I access the struct using the interface for the class?

public class Config : CurveTracer.IConfig
{

    public struct AppConfig
    {
        public string cfgVersion;
        public string cfgSerial;
     };
     public bool Init() 
}

I can get to Init(function) using IConfig.Init(). Is there a similiar way to use AppConfig. I tried IConfig.AppConfig but that does not work.

Here is IConfig

public interface IConfig
{
    bool Init();
    bool Load();
    bool LoadAppCfg();
    void LoadDefaults();
    string ReadConfigFile();
    void Save();
    void UpdateConfig(string key, string value);
    bool WriteConfigFile(string data);
}`
Was it helpful?

Solution 2

Are you thinking that Config has a property of type AppConfig? If so, you need to expose it as a property:

public class Config : CurveTracer.IConfig
{
    public bool Init()
    {
        return true;
    }

    public AppConfig AppConfig { get; set; }
}

public class CurveTracer
{
    public interface IConfig
    {
        bool Init();

        AppConfig AppConfig { get; set; }
    }
}

// Are you sure this needs to be a struct? Just add cfgVersion and cfgSerial to CurveTracer.IConfig or make it a class.
public struct AppConfig
{
    public string cfgVersion;
    public string cfgSerial;
};

OTHER TIPS

There is no way for an interface to specify that all classes implementing the interface will have an inner type.

You will need to use the concrete class's name to refer to the inner type's type name (from outside of the scope of the outer type).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top