How can I mock the assembly?

Is it possible to mock a class Assembly?

If so, with what structure and how?

If not, how would you start writing tests for code that uses Assembly?

+5
source share
1 answer

TypeMock is very powerful. I think he can do it. For other layouts like Moq or Rhino, you will need to use a different strategy.

Strategy for rhino or moka:

As an example: you use the Asssembly class to get the fully qualified name of the assembly.

public class YourClass
{
    public string GetFullName()
    {
        Assembly ass = Assembly.GetExecutingAssembly();
        return ass.FullName;
    }
}

Assembly, _Assembly. , Assembly . .

:

public class YourClass
{
    private _Assembly _assbl;
    public YourClass(_Assembly assbl)
    {
        _assbl = assbl;
    }

    public string GetFullName()
    {
        return _assbl.FullName;
    }
}

_Assembly:

public void TestDoSomething()
{
    var assbl = MockRepository.GenerateStub<_Assembly>();

    YourClass yc = new YourClass(assbl);
    string fullName = yc.GetFullName();

    //Test conditions
}
+9

All Articles