简体   繁体   English

使用 Moq 框架在 WPF MVVM 中测试 ViewModel

[英]Testing a ViewModel in WPF MVVM with Moq framework

I am new to Moq framework.我是 Moq 框架的新手。 I have a viewmodel with injected IEntityRepository我有一个注入IEntityRepository的视图IEntityRepository

public class MainViewModel
{
    private readonly IEntityRepository _entityRepository;

    public MainViewModel(IEntityRepository entityRepository)
    {
        _entityRepository = entityRepository;
        Entities = new ObservableCollection<EntityWrapper>();
    }

    public ObservableCollection<EntityWrapper> Entities { get; set; }

    public void Load()
    {
        var entities = _entityRepository.GetAll();

        Entities.Clear();

        foreach (var entity in entities)
        {
            Entities.Add(new EntityWrapper(entity));
        }
    }

    public void AddEntity(string name, int x, int y)
    {
        var entity = new Entity()
        {
            Name = name,
            X = x,
            Y = y
        };
        _entityRepository.AddEntity(entity);
        _entityRepository.Save();
    }
}

and EntityRepository is like this:EntityRepository是这样的:

public class EntityRepository : IEntityRepository
{
    private Context _context;

    public EntityRepository(Context context)
    {
        _context = context;
    }

    public Entity Get(long id)
    {
        return _context.Entities.Find(id);
    }

    public IEnumerable<Entity> GetAll()
    {
        return _context.Entities.ToList();
    }

    public void AddEntity(Entity entity)
    {
        _context.Entities.Add(entity);
    }

    public void Save()
    {
        _context.SaveChanges();
    }
}

Now, I want to test Load and AddEntity methods of MainViewModel this is my test class:现在,我想测试MainViewModel LoadAddEntity方法,这是我的测试类:

[TestClass]
public class MainViewModelTests
{
    private Mock<IEntityRepository> _mockRepo;
    private MainViewModel _mainViewModel;

    public MainViewModelTests()
    {
        _mockRepo = new Mock<IEntityRepository>();
        _mainViewModel = new MainViewModel(_mockRepo.Object);
    }

    [TestMethod]
    public void AddEntityTest()
    {
        var entity = new Entity()
        {
            Name = "Student",
            X = 10,
            Y = 20
        };

        _mainViewModel.AddEntity("Student", 10, 20);

        _mockRepo.Setup(m => m.AddEntity(entity));
        _mockRepo.Setup(m => m.Save());

        var entities = _mainViewModel.Entities;
        Assert.AreEqual(1, entities.Count);
    }

    [TestMethod]
    public void LoadTest()
    {
        // How to add fake data???

        _mockRepo.Setup(r => r.GetAll());

        _mainViewModel.Load();

        var entities = _mainViewModel.Entities;
        Assert.AreEqual(1, entities.Count);
    }
}

In AddEntityTest() I don't know why the property Entities will be empty and in LoadTest how to populate fake data so that I can load it.AddEntityTest()我不知道为什么属性Entities将为空,而在LoadTest如何填充假数据以便我可以加载它。

To get a test data in LoadTest method you can use Moq Returns method要在LoadTest方法中获取测试数据,您可以使用Moq Returns方法

_mockRepo.Setup(r => r.GetAll()).Returns(new List<Entity> { new Entity() });

It works, because List<T> implements IEnumerable<T> .它有效,因为List<T>实现了IEnumerable<T>

As for the AddEntityTest , there are a couple of issues here.至于AddEntityTest ,这里有几个问题。 You setup the mock你设置了模拟

_mockRepo.Setup(m => m.AddEntity(entity));
_mockRepo.Setup(m => m.Save());

after _mainViewModel.AddEntity("Student", 10, 20); _mainViewModel.AddEntity("Student", 10, 20); call.称呼。 It should come before, Arrange step is always before Act step, according to AAA pattern它应该来之前,安排步总是法案步骤之前,根据AAA模式

You also don't update Entities collection after adding an entity, so the Entities.Count remains the same.添加实体后,您也不会更新Entities集合,因此Entities.Count保持不变。 I suggest you to setup a IEntityRepository.GetAll() mock as well for this test and update an Entities collection in MainViewModel .我建议您为此测试设置一个IEntityRepository.GetAll()模拟并更新MainViewModelEntities集合。 Or rewrite the logic of your AddEntityTest , if collection update performed through view或者重写你的AddEntityTest的逻辑,如果集合更新通过视图执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM