我的上下文是我的数据模型的模拟 我在另一个名为“电子邮件”的类中有一个“发送”方法 我的Service类使用模拟的数据模型。 我的服务类“ SendEmailForAlarm”中的方法从Mock数据模型访问数据,然后在电子邮件类中调用“ Send”方法。 问题:如何在我的电子邮件类中使“发送”方法包含在我的模拟中?
事件代码:
//INTERFACES
public interface IEmail
{
...
void Send(string from, string to, string subject, string body, bool isHtml = false);
}
public interface IEntityModel : IDisposable
{
...
DbSet<Alarm> Alarms { get; set; } // Alarms
}
//CLASSES
public class Email : IEmail
{
...
public void Send(string from, string to, string subject, string body, bool isHtml = false)
{
...sends the email
}
}
public partial class EntityModel : DbContext, IEntityModel
{
...
public virtual DbSet<Alarm> Alarms { get; set; } // Alarms
}
public class ExampleService : ExampleServiceBase
{
//Constructor
public ExampleService(IEntityModel model) : base(model) { }
...
public void SendEmailForAlarm(string email, Alarm alarm)
{
...
new Email().Send(from, to, subject, body, true);
}
}
//HELPER method
public static Mock<DbSet<T>> GetMockQueryable<T>(Mock<DbSet<T>> mockSet, IQueryable<T> mockedList) where T : BaseEntity
{
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(mockedList.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(mockedList.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(mockedList.GetEnumerator());
mockSet.Setup(m => m.Include(It.IsAny<string>())).Returns(mockSet.Object);
// for async operations
mockSet.As<IDbAsyncEnumerable<T>>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<T>(mockedList.GetEnumerator()));
mockSet.As<IQueryable<T>>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider<T>(mockedList.Provider));
return mockSet;
}
以下单元测试通过了,但未编写来验证电子邮件是否已实际发送(或调用) service.SendEmailForAlarm方法查询EntityModel.Alarms DBSet并从检索到的对象中提取信息。然后,它在电子邮件类中调用Send方法,并返回void。
[TestMethod]
public void CurrentTest()
{
//Data
var alarms = new List<Alarm> {CreateAlarmObject()} //create a list of alarm objects
//Arrange
var mockContext = new Mock<IEntityModel>();
var mockSetAlarm = new Mock<DbSet<Alarm>>();
var service = new ExampleService(mockContext.Object);
mockSetAlarm = GetMockQueryable(mockSetAlarm, alarms.AsQueryable());
mockContext.Setup(a => a.Alarms).Returns(mockSetAlarm.Object);
//Action
service.SendEmailForAlarm("test@domain.com", alarms[0]);
//NOTHING IS ASSERTED, SendEmailForAlarm is void so nothing returned.
}
我想做的是将一个Interceptor放在Email.Send方法上,这样我就可以计算它被执行了多少次,或者通过模拟验证来验证它被调用了n次(在此示例中为一次)。
[TestMethod]
public void WhatIWantTest()
{
//Data
var alarms = new List<Alarm> {CreateAlarmObject()} //create a list of alarm objects
var calls = 0;
//Arrange
var mockEmail = new Mock<IEmail>(); //NEW
var mockContext = new Mock<IEntityModel>();
var mockSetAlarm = new Mock<DbSet<Alarm>>();
var service = new ExampleService(mockContext.Object);
mockEmail.Setup(u =>
u.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<bool>()))
.Callback(() => calls++); //NEW add a call back on the Email.Send method
mockSetAlarm = GetMockQueryable(mockSetAlarm, alarms.AsQueryable());
mockContext.Setup(a => a.Alarms).Returns(mockSetAlarm.Object);
//Action
service.SendEmailForAlarm("test@domain.com", alarms[0]);
//Assert NEW
Assert.AreEqual(1, calls); //Check callback count to verify send was executed
//OR
//Get rid of method interceptor line above and just verify mock object.
mockEmail.Verify(m => m.Send(It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<bool>()), Times.Once());
}
当我运行此命令时,断言失败,因为“呼叫” = 0,Times.Once也为零。 我相信我的问题是由于使用“ mockContext”(IEntityModel)创建了“服务”这一事实,而访问警报数据则需要该服务,但是Send方法不是模拟上下文的一部分。如果我模拟“ mockEmail”并添加回调,则无法弄清楚如何将其添加到模拟上下文。
如何将Mock IEmail和Mock IEntityModel都添加到SAME上下文中?还是我要解决所有这些错误?
答案 0 :(得分:2)
仅需澄清一下:您正在创建一个Mock,但是没有没有告诉代码使用将此模拟用于任何东西,据我所知。您不会在任何地方使用该模拟-只需创建它,然后再进行测试即可;毫不奇怪,它没有被调用。
我认为您遇到的问题是您的[Employees]
[Name] [job] [ID]
Mike Pilot 1
Goku Warrior 2
实例化了一个新的ExampleService
类:
Email()
更好的方法是在构造函数中提供new Email().Send(from, to, subject, body, true);
:
IEmail
然后在以后使用它:
IEmail _email;
//Constructor
public ExampleService(IEntityModel model, IEmail email) : base(model)
{
_email = email;
}
然后您可以将 public void SendEmailForAlarm(string email, Alarm alarm)
{
_email.Send(from, to, subject, body, true);
}
注入Mock<IEmail>
中,并检查它是否被适当调用。
如果很难/不可能为构造函数注入正确设置DI,那么您可以可以具有一个公共属性,该属性可以让您用另外一个替换类中的IEmail。用于单元测试,但这有点麻烦:
ExampleService
然后在您的测试中:
public class ExampleService
{
private IEmail _email = null;
public IEmail Emailer
{
get
{
//if this is the first access of the email, then create a new one...
if (_email == null)
{
_email = new Email();
}
return _email;
}
set
{
_email = value;
}
}
...
public void SendEmailForAlarm(string email, Alarm alarm)
{
//this will either use a new Email() or use a passed-in IEmail
//if the property was set prior to this call....
Emailer.Send(from, to, subject, body, true);
}
}
如果这样的 //Arrange
var mockEmail = new Mock<IEmail>(); //NEW
var mockContext = new Mock<IEntityModel>();
var mockSetAlarm = new Mock<DbSet<Alarm>>();
var service = new ExampleService(mockContext.Object);
//we set the Emailer object to be used so it doesn't create one..
service.Emailer = mockEmail.Object;
属性是个问题,您可以将其设置为public
,然后使用internal
(google)在测试项目中使其可见(尽管这仍然会对于包含服务本身的项目中的其他类是可见的。.