locked
Testing htm pages & web services in Nunit RRS feed

  • Question

  • I want to have consistent nunit tests between an MVC application's screens & its API. EG  "/api/values"  and "/Person/index"  both return the same data, the first as JSON, & the second as a html page. In fact the PersonController uses "/api/values" to get its data, which is in a SQL Server local database

    ( from web.config)
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MVCWebApi-20130902092231;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MVCWebApi-20130902092231.mdf" />
     
    The api & the web pages work fine as urls in debug mode outside of nunit & an nunit test of the api works fine. but the unit test of the html page fails with System.Data.ProviderIncompatibleException

    I would have thought that the html page tests would only have needed to know about the index function in the PersonController, & let that worry about finding the service, & the service worry about finding the data.  But clearly not !  

    What preconfig & [setup] options would I need for the html page to work:

    Here's the working test for api/values:

    [TestFixture()]
        public class UnitTest1
        {
            private string UrlBase = " http://localhost:1234" ;

           private string servicePath = "api/values";
            private HttpClient client;

            [SetUp]
            public async void SetUP()
            {
                client = new HttpClient();
                client.BaseAddress = new Uri(UrlBase);
                response = client.GetAsync(servicePath).Result;
            }
            [Test]
            public void GetResponseIsSuccess()
            {
                 Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);       
            }
        }

    Here's the [test] from the htmlpage test ( there is no [Setup] section , but it is sucessfully finding the webservice :  Api/values, its just that  db.Get() is failing in the webservice

        [Test]
            public void PersonIndexTest()
            {
                string expected = "Index";
                PersonController controller = new PersonController();
                var result = controller.Index() as ViewResult;
                Assert.AreEqual(expected, result.ViewName);
            }
    I am not sure what I need to do to get the service working in this case.    Most examples I have seen of testing front ends, build a mock data object on the fly, rather than retrieve data from a service.  

    Here's the relevant application code :

    \API\ValuesController.cs

    namespace MVCWebApi.Controllers
    {
        public class ValuesController : ApiController
        {
            PersonEntities db = new PersonEntities();
            public IEnumerable<Person> Get()
            {
    // code where error occurs in nunit html test
                return db.Persons.ToList();         
            }     
        }
    }


    \Controllers\PersonController.cs
    namespace MVCWebApi.Controllers
    {
        public class PersonController : Controller
        {
            ValuesController db = new ValuesController();
            public ActionResult Index()
            {
                var list = db.Get(); //Getting List of records from web API
                return View(list);
            }
    }}


    \Models\Person.cs
    namespace MVCWebApi.Models
    {
        public class Person
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
        public class PersonEntities : DbContext
        {
            public DbSet<Person> Persons { get; set; }
        }
    }


    • Edited by RSRSRSRSRSRS Wednesday, October 1, 2014 12:26 PM
    Wednesday, October 1, 2014 12:25 PM

Answers

  • Hi,

    For this issue, the reason is that for the way to call the method of controller directly, it uses the test project’s configure file (app.config) instead of the web project’s configure file (web.config). In other word, it will uses the connectionstring that in the test project’s configure file. Since there isn’t the matched connectionstring in the test project’s configure file (app.config), it will failed to get the data from database.

    However for the test of web API, you are using HttpClient to send the request, this is the HTTP request. So, the web project still use its own configure file.

    To deal with this issue, you need to copy connectionstring that in web.config file to test project’s app.config file.

    Best Regards

    Starain Chen


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 2, 2014 5:40 AM