我的 MVC 控制器应该测试数据库中的 return <empty> 对象吗?难道我做错了什么?
Should my MVC Controller tests return <empty> Objects from the database? Am I doing something wrong?
我正在测试一些 MVC 控制器。我对这种特殊的测试方法比较陌生。当我模拟控制器和方法属性时,然后执行方法,方法returns <empty>
。所以要么它应该这样做,要么我没有连接到数据库。我在 app.config 文件中添加了一些连接字符串和数据源,但没有成功。还是returns<empty>
我的 App.Config 文件(片段)
<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>
我的测试 - Returns <empty>
当 Assert.AreSame 被称为 .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);
}
}
我的控制器
[HttpPost]
[ActionName("RetrieveContacts")]
public List<Contact> GetContacts([FromBody]string query)
{
var context = GetContext();
return _contactManager.GetContacts(context.SiteLocationCode, query);
}
不确定您要测试的是什么。如果您模拟接口,它将不会使用您指定的任何数据库连接字符串。它是一个伪造的 class ,它模仿界面而不做任何实际工作,只是为了让您可以传递给需要对象类型的函数。如果您想使用真实数据库,请不要使用模拟。
我正在测试一些 MVC 控制器。我对这种特殊的测试方法比较陌生。当我模拟控制器和方法属性时,然后执行方法,方法returns <empty>
。所以要么它应该这样做,要么我没有连接到数据库。我在 app.config 文件中添加了一些连接字符串和数据源,但没有成功。还是returns<empty>
我的 App.Config 文件(片段)
<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>
我的测试 - Returns <empty>
当 Assert.AreSame 被称为 .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);
}
}
我的控制器
[HttpPost]
[ActionName("RetrieveContacts")]
public List<Contact> GetContacts([FromBody]string query)
{
var context = GetContext();
return _contactManager.GetContacts(context.SiteLocationCode, query);
}
不确定您要测试的是什么。如果您模拟接口,它将不会使用您指定的任何数据库连接字符串。它是一个伪造的 class ,它模仿界面而不做任何实际工作,只是为了让您可以传递给需要对象类型的函数。如果您想使用真实数据库,请不要使用模拟。