0

I am testing some MVC Controllers. I am relatively new to this particular method of testing. When I mock the controller and method properties, then execute the method, the method returns <empty>. So either it is supposed to do that or I am not connected to the database. I added some connection strings and data-sources to my app.config file and no luck. Still returns <empty>

My App.Config file (snippet)

<connectionStrings>
<!-- <add name="DbContexy" providerName="System.Data.SqlClient" connectionString="Persist Security Info=False;User ID=user;Password=Password;Initial Catalog=Db;Data Source=MySource" />-->
<!-- Inserted Connection String Below -->
<add name="DbContext" providerName="System.Data.SqlClient" connectionString="user id=User;password=hello;Data Source=exampleSource;Database=MyDb" />
<!--<add name="DbContext" providerName="System.Data.SqlClient" connectionString="Persist Security Info=False;User ID=user;Password=password;Initial Catalog=Sb;Data Source=.\MySource" />-->
</connectionStrings>

My Test - Returns <empty> when Assert.AreSame is called INSTEAD of .AreEqual

[Test]
public void GetContacts_ReturnContacts()
{
    //Arrange
    var mockContactManager = A.Fake<IContactManager>();
    var mockContext = A.Fake<CallerInfo>();
    var mockCallerInfoManager = A.Fake<ICallerInfoManager>();
    var mockSiteRepository = A.Fake<ISiteRepository>();
    var mockContactController = A.Fake<ContactController>();
    mockContext.SiteLocationCode = "US1";
    const int mockContactId = 168;

mockContext.ContactId = mockContactId;

List<Contact> expected = new List<Contact> { }; // What we expect to get back

A.CallTo(() => mockContactManager.GetContacts(mockContext.SiteLocationCode)).Returns(expected);

using (mockContactController = new ContactController(mockContactManager, mockCallerInfoManager, mockSiteRepository))
{
    //Act
    List<Contact> returnedContacts = mockContactController.GetContacts();

    //Assert
Assert.AreEqual(expected, returnedContacts);
    }
    }

My Controller

  [HttpPost]
    [ActionName("RetrieveContacts")]
    public List<Contact> GetContacts([FromBody]string query)
    {
        var context = GetContext();           
        return _contactManager.GetContacts(context.SiteLocationCode, query);
    }
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
jward01
  • 750
  • 4
  • 11
  • 26

1 Answers1

2

Not sure what you are trying to test. If you mocked the interface it will not use any database connection strings your specified. It is a fake class that imitates the interface without doing any real work just so you can pass to the functions that expect the type of object. If you want to use real database then don't use mocks.

Woland
  • 2,881
  • 2
  • 20
  • 33
  • Thank you! That answers alot of questions. Nice explaination. I am trying to test the 'GetContacts' Controller Method. --I am new, Am I doing it right? Thanks! – jward01 Jan 24 '16 at 21:35
  • 2
    There is really no logic in GetContacts method, so it seems there is nothing to test there. It would make sense to create the actual IContactManager instance that connects to the database and pass it along to the ContactController instance together with actual repository implementation. If however you do plan to have logic in GetContacts method, then you can use mocks, but make sure to mock the methods of repository to return some sort of data (without connecting to the database). – Woland Jan 24 '16 at 21:44