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);
}