locked
Referencing an MVC project from a unit test project. RRS feed

  • Question

  • User269416624 posted

    I have a project that I'm placing all my unit tests into.  There are some functions inside my actual MVC website project that I want to test.  Both these projects are part of the same solution - along with some other support projects. 

    I had to add references to System.Web and System.Web.Mvc (et. al.) to my test project in order to get the test project to recognize the MVC project.  However, it only sees certain namespaces within the MVC project - and of course the ones that I want to test are not the ones visible.  :(

    So is it just a bad idea to try testing the MVC project from another project?  Should I just put my tests for the MVC project inside that project?

    Or is there something that I'm missing here?

    Tuesday, February 9, 2016 11:47 PM

Answers

  • User269416624 posted

    I had a number of references that I had to get added to the test project to support the MVC project - the 'warnings' were very helpful with identifying those - but still the problem persisted.

    Finally discovered that if I add the name of the MVC assembly to the code module as a 'using' statement, then I have full visibility into the assembly.  But if I try fully qualifying the path to the functions by putting the assembly name first, it doesn't work.

    So...

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Web.Mvc;
    using Mss3CsMvc;
    
    namespace WebMssUnitTests.website.Lib
    {
        [TestClass]
        public class HelpersUnitTests
        {
    
            [TestMethod]
            public void checkbox_test()
            {
    
                //arrange
                string expectedValue = "<input name=\"test\" type=\"checkbox\"></input>";
                string actualValue;
    
                //act
                actualValue = customHtml.Helpers.Checkbox("test", "").ToString();
    
                //assert
                Assert.AreEqual(expectedValue, actualValue);
    
            }
        }
    }
    

    Works. 

    But...

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Web.Mvc;
    
    namespace WebMssUnitTests.website.Lib
    {
        [TestClass]
        public class HelpersUnitTests
        {
    
            [TestMethod]
            public void checkbox_test()
            {
    
                //arrange
                string expectedValue = "<input name=\"test\" type=\"checkbox\"></input>";
                string actualValue;
    
                //act
                actualValue = Mss3CsMvc.customHtml.Helpers.Checkbox("test", "").ToString();
    
                //assert
                Assert.AreEqual(expectedValue, actualValue);
    
            }
        }
    }
    

    Does not.

    Weird.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 6:09 PM

All replies

  • User197322208 posted

      However, it only sees certain namespaces within the MVC project - and of course the ones that I want to test are not the ones visible.  :(

    You do not test namespaces - you test classes and/or functions.

    Maybe those are private. 
    On other hand, please read http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx to understand testing MVC .

    Wednesday, February 10, 2016 4:12 AM
  • User269416624 posted

    I can see how my wording on that may have been confusing, but I'm certainly looking to test the classes and functions within those namespaces - namespaces have nothing to test.

    All the classes and functions within the namespaces in question are public.

    I'll take a look at that article.

    Wednesday, February 10, 2016 8:05 PM
  • User269416624 posted

    I had a number of references that I had to get added to the test project to support the MVC project - the 'warnings' were very helpful with identifying those - but still the problem persisted.

    Finally discovered that if I add the name of the MVC assembly to the code module as a 'using' statement, then I have full visibility into the assembly.  But if I try fully qualifying the path to the functions by putting the assembly name first, it doesn't work.

    So...

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Web.Mvc;
    using Mss3CsMvc;
    
    namespace WebMssUnitTests.website.Lib
    {
        [TestClass]
        public class HelpersUnitTests
        {
    
            [TestMethod]
            public void checkbox_test()
            {
    
                //arrange
                string expectedValue = "<input name=\"test\" type=\"checkbox\"></input>";
                string actualValue;
    
                //act
                actualValue = customHtml.Helpers.Checkbox("test", "").ToString();
    
                //assert
                Assert.AreEqual(expectedValue, actualValue);
    
            }
        }
    }
    

    Works. 

    But...

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Web.Mvc;
    
    namespace WebMssUnitTests.website.Lib
    {
        [TestClass]
        public class HelpersUnitTests
        {
    
            [TestMethod]
            public void checkbox_test()
            {
    
                //arrange
                string expectedValue = "<input name=\"test\" type=\"checkbox\"></input>";
                string actualValue;
    
                //act
                actualValue = Mss3CsMvc.customHtml.Helpers.Checkbox("test", "").ToString();
    
                //assert
                Assert.AreEqual(expectedValue, actualValue);
    
            }
        }
    }
    

    Does not.

    Weird.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 6:09 PM