使用 Specflow 通过 Moq 模拟控制器依赖关系

发布于 2024-10-14 01:56:12 字数 386 浏览 4 评论 0原文

我是 Specflow 的新手,对如何模拟我的内容有疑问 控制器依赖性。 例如我有一个 UserController 类,它取决于我的 UserRepository 类,它传递给其上的控制器类 构造函数。 所以使用 Moq 我正在做这样的事情:

var mock = new Mock<UserRepository>();
mock.Setup(m => m.ListAll()).Returns(new List<User>());
var browser = new IE(string.Format("http://localhost:4265/{0}",
username));

但是我的控制器没有使用模拟对象,我应该怎么做 那?

谢谢

I am new to specflow and a have a doubt about how to mock my
controller dependencies.
For instance I have a UserController class which depends on my
UserRepository class that a pass to the controller class on its
constructor.
So using Moq I am doing something like this:

var mock = new Mock<UserRepository>();
mock.Setup(m => m.ListAll()).Returns(new List<User>());
var browser = new IE(string.Format("http://localhost:4265/{0}",
username));

But my controller is not using the mocked object, how should I do
that?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-10-21 01:56:13

您没有对模拟执行任何操作以将其注入控制器。您的控制器需要获得用户存储库才能使用。

您还需要接受更多答案。

You are not doing anything with the mock to inject it into your controller. Your controller needs to be given the user repository in order for it to be used.

Also you need to accept more answers.

倚栏听风 2024-10-21 01:56:12

您正在混合三个(至少)测试框架,这当然很酷,但您可能应该停下来考虑一下您想要测试的内容。

Watin 非常适合测试您的 UI,因为它控制浏览器实例。我发现它擅长进行回归测试 http://en.wikipedia.org/wiki/Regression_testing

Specflow 也很棒——我个人喜欢用它来缩小业务开发人员和(我们)软件开发人员之间的差距,因为我们实际上可以用我们都理解的术语(也可能是组织的其他部分)来定义需求,但我不知道我不想发起一场口水战,但它可能会带来比解决的问题更多的问题,除非你关注它的真正价值。我们在工作中通过测试服务层(表示层中控制器下面的一层)来使用它,实际上我们只模拟数据库、外部服务和文件系统等——这使得我们的 Specflow 测试成为某种集成测试。

Moq 是一个模拟框架,当然可以用于任何类型的测试(就像我只是让它溜走),但这是一个非常好的单元测试工具。

那么回到你的问题。如果您想进行一次测试来找到所有错误,那么您就有麻烦了;)我知道您不希望那样 - 这只是我提出的一个愚蠢的建议 - 但实际上,如果您只想进行集成测试(从 UI 向下运行几个层/依赖项的测试)您可以像现在一样轻松地混合不同的测试框架,但是为什么要模拟用户存储库呢?那是因为你不想访问数据库吗?

无论如何,进行您想要的集成测试的一种方法是将您的解决方案配置为使用模拟 - 或者也许可以使用存根(创建一个假用户存储库来返回您想要测试的数据) - 您应该使用依赖框架,如 Unity、Ninject 或结构图(男孩,我们不要开始争论使用什么框架)并且有Watin 正在使用测试 url 使用带有 fake/mock 存储库的配置来启动您的网站。

另一方面,您可以对控制器、服务等进行单元测试。您甚至可能想尝试 TDD< /a> 但这是另一章,我不能在这里介绍!

You are mixing three (atleast) test framework, which ofcourse is cool, but you should probably stop and consider what it is you want to test.

Watin is good for testing your UI as it controls a browser instance. I find it good at making regression tests http://en.wikipedia.org/wiki/Regression_testing

Specflow is great as well - personally i like to use it for closing the gap between business developers and (us) software developers as we can actually define requirements in terms we both understand (and probably other parts of the organization as well) I don't want to start a flame war, but it can introduce more problems than it solves, unless you focus on its real values. We use this at work by testing the service layer (one layer below the controllers in the presentation layer) and we actually only mock the database, external services and file system etc - which makes our specflow tests some kind of integration tests.

Moq is a mocking framework and can ofcourse be used in any kind of tests (like i just let it slip we do) but this is such a great tool for unit testing.

So to return to your question. If you want to make one test to find all your bugs, you're in trouble ;) I know you don't want that - that was just a silly suggestion i made - but really, if you just want to do integration tests (tests running from the UI down through several layers/dependencies) you could easily mix different testing frameworks like you are now, but then why mock the user repository? Is that because you don't want to hit the database?

Anyways one way to do the integration test you seem like you want would be to configure your solution to use a mock - or perhaps a stub would do (create a fake userrepository that returns the data you want to test with) - you should use a Dependency framework like Unity, Ninject or structure map (boy let's not start a war about what framework to use) and have the test url Watin is using launch your site using a configuration with the fake/mock repositories.

You could on the other hand do unit testing on your controllers, services etc. You might even want to try out TDD but that's a whole other chapter i can't cover here!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文