S#arp 架构中的测试状态 - 最佳实践

发布于 2024-10-04 09:05:18 字数 1142 浏览 2 评论 0原文

如何测试 S#arp 架构项目中的状态?

例如,我有一个自定义 RoleProvider。我想测试方法provider.AddUsersToRoles(string[], string[])。

所以我从以下开始:

// Arrange
const string ficticiousRole = "Management";
var userToExpect = UserInstanceFactory.CreateValidTransientUser();
var roleToExpect = RoleInstanceFactory.CreateValidTransientRole();

userRepository.Expect(r => r.GetByUsername(userToExpect.Username))
              .Return(userToExpect);
roleRepository.Expect(r => r.GetByName(ficticiousRole))
              .Return(roleToExpect);

var userNames = new List<string>();
var roleNames = new List<string>();
userNames.Add(userToExpect.Username);
roleNames.Add(ficticiousRole);

然后将用户添加到角色。然后我检查用户是否属于该角色。

// Act
roleProvider.AddUsersToRoles(userNames.ToArray(), roleNames.ToArray());
var isNewUserInRole = roleProvider.IsUserInRole(userToExpect.Username, ficticiousRole);

// Assert
Assert.IsTrue(isNewUserInRole);

问题是我正在使用 Rhino Mocks。我对 Rhino Mocks 的了解有限,但据我了解(根据 Ayende Rahien 的说法),你使用 Rhino Mocks 来测试操作,而不是状态。

所以我想内存中的 SqlLite 数据库会更合适?在 S#arp Arch 中执行此操作的最佳方法是什么?

How do I test for state in a S#arp Architecture project?

For example, I have a custom RoleProvider. I want to test the method provider.AddUsersToRoles(string[], string[]).

So I start with:

// Arrange
const string ficticiousRole = "Management";
var userToExpect = UserInstanceFactory.CreateValidTransientUser();
var roleToExpect = RoleInstanceFactory.CreateValidTransientRole();

userRepository.Expect(r => r.GetByUsername(userToExpect.Username))
              .Return(userToExpect);
roleRepository.Expect(r => r.GetByName(ficticiousRole))
              .Return(roleToExpect);

var userNames = new List<string>();
var roleNames = new List<string>();
userNames.Add(userToExpect.Username);
roleNames.Add(ficticiousRole);

Then I add the user to a role. Then I check if the user is in this role.

// Act
roleProvider.AddUsersToRoles(userNames.ToArray(), roleNames.ToArray());
var isNewUserInRole = roleProvider.IsUserInRole(userToExpect.Username, ficticiousRole);

// Assert
Assert.IsTrue(isNewUserInRole);

The problem is that I'm using Rhino Mocks. I have limited knowledge of Rhino Mocks, but from what I understand (according to Ayende Rahien) you use Rhino Mocks to test for operations, not state.

So I guess an in-memory SqlLite db would be more suitable? What's the best way to do this in S#arp Arch?

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

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

发布评论

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

评论(2

-黛色若梦 2024-10-11 09:05:18

你不能用 Rhino Mocks 做到这一点,因为那只是一个模拟框架,可以伪造数据库调用等内容。听起来你实际上想测试数据库的持久性,这基本上是数据库集成测试。在这种情况下,您肯定会希望使用内存数据库,例如 SqlLite(如果可能的话!),而不是使用 SQL Server 实例。

您想要做的是在每个测试或测试类开始时拆除数据库(如果已存在),重建数据库,用一些种子数据填充数据库,然后测试数据库交互。这样,您可以确保在运行每个测试之前拥有已知的数据库状态。

我在一个项目中所做的一件事是将所有只读测试分组到一个测试类中,这样我只需为该类执行一次数据库重建步骤,并将所有删除、更新和插入测试移动到其他测试类中在每次测试之前重建数据库。如果进行了足够的测试,这可能会非常耗时,并且可能需要将其降级到 CI 服务器。

You can't do that with Rhino Mocks since that is simply a mocking framework that fakes things like database calls, etc. It sounds like you actually want to test persistence to the database which is basically database integration testing. In that case, you would most definitely want to use an in-memory database such as SqlLite (if possible!) as opposed to hitting a SQL Server instance.

What you want to do is at the beginning of each test or test class is tear down the database if it exists already, rebuild the database, populate the database with some seed data and then test your database interaction. This way, you can ensure you have a known database state before each test is run.

One thing I did on a project was grouped all of my read-only tests into one test class so that I only had to do the database rebuild step once for the class and moved all my delete, update and insert tests into other test classes that rebuilt the database before each and every test. Given enough tests, this can be quite time consuming and may want to be relegated to CI server.

你的背包 2024-10-11 09:05:18

如果没有看到其余代码,就很难回答这个问题。

Without seeing the rest of your code, it would be difficult to answer this.

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