在 MVVM 中为 WPF 使用 specflow [英] Using specflow for WPF in MVVM

查看:62
本文介绍了在 MVVM 中为 WPF 使用 specflow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用 MVVM 模式的 WPF 项目的 specflow.所以,我有存储库、控制器、视图和视图模型.Repository 调用 webservice 至命中数据库并返回数据.视图模型具有验证用户输入和调用存储库方法的方法.

I am trying specflow for the first time for a WPF project with MVVM pattern. So, I have repository, controller, view and view model. Repository makes calls to webservice wich hits the database and gives back the data. View model has methods to validate the input from the user and make calls to repository methods.

在我的规范流中,我应该进行完整的调用,包括存储库方法还是应该使用 Moq 模拟这些方法?有意义吗?

In my specflow, should I be making a complete call including the repository methods or should I mock those methods using Moq? Does it make sense?

推荐答案

简短的回答:你可以让你的调用命中数据库,但我建议你模拟它们,除非你的数据库中有逻辑

The short answer: You could make you calls hit the database, but I would recommend mocking them out, unless you have logic in your DB

长答案:有两种相互竞争的方式来看待这个问题.首先,您希望使用 SpecFlow 进行什么样的测试,其次,访问您的数据库有多容易.

The long answer: There's two competing ways of looking at this question. Firstly what kind of testing are you expecting to use SpecFlow for, and secondly how easy is it going to be to access your database.

如果您使用 SpecFlow 来测试您的低级技术要求,那么您实际上是在以示例说明风格进行单元测试.所以这真的应该决定你使用模拟来隔离你的单位.(我个人会坚持使用 NUnit 进行这些测试.)

If you are using SpecFlow to test your low level technical requirements, then you really are doing unit testing in a Specification by example style. So this really ought to dictate that you use mocking to isolate your units. (Personally I'd stick to using NUnit for these tests.)

然而,如果您使用 SpecFlow 来测试您的业务场景(即验收测试),那么您的场景依赖于多个单元来提供功能.这更像是集成或系统测试,因此需要涉及许多组件,理论上因为我们正在测试整个系统,我们应该在其中包含一个数据库,特别是如果您甚至有一个您可能想要的存储过程或视图稍后进行回归测试.

If however you are using SpecFlow to test your business scenarios (i.e. Acceptance testing) then your scenarios are reliant on more than one unit to provide the functionality. This is more like Integration or System testing and so many components will need to be involved and in theory since we are testing the whole system, we should include a DB in that, particularly if you have even one stored procedure or view that you might want to regression test later.

然而,第二种看待这个问题的方法是拥有一个可用于测试的数据库需要多少痛苦.

However, the second way of looking at this is how much pain is required to have a database available for your testing.

  • 如果您的开发团队中有不止一个人,如果您同时运行测试,您需要采取哪些策略来避免失败?
    • 例如,您可以在每次运行中生成一个新的唯一客户,并且只访问针对该客户的更新和查询,这样您就不会得到错误数量的订单.
    • 例如如果您使用正确类型的数据库,您可以将一个空的 .mdb 签入您的源代码管理,然后回滚/恢复到那个.

    我个人发现,对于没有存储过程和视图的简单数据库,模拟更好,但是一旦您将业务逻辑添加到数据库中,您就需要以某种方式测试该逻辑.

    I've personally found that for a simple DB without stored procs and views, mocking is better, but as soon as you've added business logic into the DB, well, you need to test that logic somehow.

    这篇关于在 MVVM 中为 WPF 使用 specflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆