How to enter assembly layout for use with Moq

My controller has a method that returns attribute data from the current executing assembly in a partial view.

In this example, I just occupy the name, but I need to do more with it.

Controller:

    var title = "";

    var asm = Assembly.GetExecutingAssembly();
    var attrs = asm.GetCustomAttributes(typeof(AssemblyTitleAttribute));
    var titleAttr = (AssemblyTitleAttribute)attributes[0];

    title = titleAttr.Title;

    return PartialView("_Build", title);

When writing unit test in Moq, I need to find a way to insert Assembly attributes into the layout so that I can make sure that the correct attributes are generated when the controller test runs, and then do x or y with my statements.

UnitTest:

    //Arrange
    //The magic I need to happen.

    //Act
    var controller = GetController();
    var result = controller.MyMethod() as PartialViewResult;
    var title = result.Model;

    //Assert
    Assert.AreEqual("Title", title); //currently static, need to verify against a mock

I know this is a very simple set of code, but I just need proof of concept at the moment.

Is there a good way to create a fake Assembly? Do I need to use System.Reflection.Emit?

+4
2

, . GetCustomAttributes, . .

:

private IDependency _someDependency;

public HomeController(IDependency someDependency)
{
    _someDependency = someDependency;
}

public ActionResult MyMethod()
{
    var title = "";
    var version = "";

    IEnumerable<Attribute> attributes = GetCustomAttributes(typeof(AssemblyVersionAttribute)).ToList();
    AssemblyVersionAttribute verAttr = attributes.OfType<AssemblyVersionAttribute>().FirstOrDefault();
    if (verAttr != null) version = verAttr.Version;

    attributes = GetCustomAttributes(typeof(AssemblyTitleAttribute)).ToList();
    AssemblyTitleAttribute titleAttr = attributes.OfType<AssemblyTitleAttribute>().FirstOrDefault();
    if (titleAttr != null) title = titleAttr.Title;

    return PartialView("_Build", title + version);
}

public virtual IEnumerable<Attribute> GetCustomAttributes(Type attributeType)
{
    var asm = Assembly.GetExecutingAssembly();
    var attrs = asm.GetCustomAttributes(attributeType);
    return attrs;
} 

:

[TestClass]
public class MyMethodTest
{
    [TestMethod]
    public void MyMethod_WhenCalled_PartialViewIsReturned()
    {
        // Arrange
        // The magic I need to happen.

        Mock<IDependency> dependencyStub = new Mock<IDependency>();
        // dependencyStub.Setup(...).Returns(...);
        var controller = new TestableHomeController(dependencyStub.Object)
        {
            UseFakeAttributes = true
        };

        // Act
        var result = controller.MyMethod() as PartialViewResult;

        // Assert
        var model = result.Model;
        Assert.AreEqual("MyFakeTitle1.0.0.0", model); // currently static, need to verify against a mock
    }

    private class TestableHomeController : HomeController
    {
        public bool UseFakeAttributes { get; set; }

        public TestableHomeController(IDependency someDependency)
            :base(someDependency)
        { }

        public override IEnumerable<Attribute> GetCustomAttributes(Type attributeType)
        {
            return UseFakeAttributes
                ? new List<Attribute>
                    {
                        new AssemblyTitleAttribute("MyFakeTitle"),
                        new AssemblyVersionAttribute("1.0.0.0"),
                        new AssemblyDescriptionAttribute("Assembly fake description")
                        // next attributes ...
                    }.Where(a => a.GetType() == attributeType)
                : base.GetCustomAttributes(attributeType);
        }
    }
}
+1

, . (, ) . , , , . , - , , , "build" "title", .

, , . - ; .

0

All Articles