我有一个使用方法GetAll
(显示所有面霜)的控制器
public class AdminController : Controller
{
private readonly ICreamUOW creamUOW;
public AdminController( ICreamUOW creamUOW)
{
this.creamUOW = creamUOW;
}
[HttpGet]
[Authorize(Roles = "Administrator")]
public PartialViewResult TableCreams()
{
return PartialView(creamUOW.Creams.GetAll.ToList());
}
}
我意识到我的存储库的工作单元模式
public class CreamUOW : ICreamUOW
{
private readonly CreamEFDbContext contextDb;
private CreamRepository creamRepository;
public CreamUOW()
{
this.contextDb = new CreamEFDbContext();
}
//properties
public CreamRepository Creams
{
get
{
if (creamRepository == null)
creamRepository = new CreamRepository(contextDb);
return creamRepository;
}
}
}
和他的界面
public interface ICreamUOW : IDisposable
{
CreamRepository Creams { get; }
}
我通过ninject IoC绑定此类和接口
kernel.Bind<ICreamUOW>().To<CreamUOW>();
(我只显示问题所在的方法和属性,我在项目中实现了处置方法,但现在并不重要)
我的通用存储库界面
public interface ICreamRepository<T> where T : class
{
//property
IEnumerable<T> GetAll { get; }
}
和他的认识
public class CreamRepository : ICreamRepository<CreamModel>
{
private CreamEFDbContext context;
public CreamRepository(CreamEFDbContext dbContext)
{
context = dbContext;
}
public IEnumerable<CreamModel> GetAll
{
get { return context.CreamModels.Include(x => x.CreamTypeModel); }
}
}
我尝试进行测试,但不起作用
[TestMethod]
public void TableCreamContainCreams()
{
//arrange
List<CreamModel> creams = new List<CreamModel>()
{
new CreamModel () { Id = 1, Name = "Test te1", Description = "1" },
new CreamModel () { Id = 2, Name = "Test te2", Description = "2" }
};
private Mock<ICreamUOW> mockCreamUOW = new Mock<ICreamUOW>();
mockCreamUOW.Setup(uow => uow.Creams.GetAll).Returns(creams.ToList());
AdminController controller = new AdminController(null, null, mockCreamUOW.Object);
//action
PartialViewResult resultView = controller.TableCreams();
//assert
Assert.AreEqual(((List<CreamModel>)resultView.Model).Count(), 2);
Assert.IsTrue(((List<CreamModel>)resultView.Model).Count(p => p.Description == "1") == 1);
}
我接受
消息:测试方法UnitTests.TestAdminController.TableCreamContainCreams引发异常: System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置:uow => uow.Creams.GetAll
这是什么意思,以及如何编写正确的测试?有人可以帮忙吗?
答案 0 :(得分:1)
您只能使用Moq来模拟接口和虚拟方法。
我认为我们有2种选择。
一个是将 <script>
$(function () {
$('form').on('submit', function (e) {alert(123);
e.preventDefault();
$.ajax({
type: 'post',
url: 'receive-callback.php',
data:{"name":name,"email":email},
success: function () {
if(result == 0){
$('.signup_side').fadeOut(500).promise().done(function() {
$('.signup_side').load('do_signup.php',function(){}).hide().fadeIn(500);
});
}else{
$('.signup_side').fadeOut(500).promise().done(function() {
$('.signup_side').load('login.php',function(){}).hide().fadeIn(500);
});
}
}
});
});
});
</script>
关键字添加到virtual
的{{1}}属性中。
GetAll
然后在单元测试中添加 CreamRepository 的另一个 Mock 。
CreamRepository
两个是使public virtual IEnumerable<CreamModel> GetAll
{
get
{
return context.CreamModels.Include(a => a.CreamTypeModel);
}
}
的{{1}}方法和// A mock of CreamRepository, with null passed in because the constructor wants it
Mock<CreamRepository> mockCreamRepository = new Mock<CreamRepository>(null);
// which will return fake data
mockCreamRepository.Setup(mcr => mcr.GetAll).Returns(creams);
// Calling Creams on Mock UOW will give us Mock CreamRepository
// which will in turn give us the fake data if its GetAll is called.
mockCreamUOW.Setup(uow => uow.Creams).Returns(mockCreamRepository.Object);
返回GetAll
ICreamUOW
还有您的单元测试
CreamUOW
但是,正如@sellotape指出的那样,并且我对您的UOW和存储库的实现不熟悉,这肯定有问题,因为如果我们正在测试一段调用A层的代码,然后再调用B层,我们只需要模拟A层即可。
让我知道是否有帮助。