ASP.net MVC RTM 测试命名约定

发布于 2024-08-06 09:21:53 字数 842 浏览 7 评论 0原文

我正在开发一个 asp.net mvc 应用程序并编写我的单元测试 BDD 风格。 例如。

GetResource_WhenResourceFileExists_ShouldReturnResources()

但是当我为控制器编写测试时,我通常有两个同名的方法。一种不带参数用于获取请求,另一种带参数用于帖子。有人有一个很好的命名约定来区分两者吗?

我能想到:

1.
LogIn_WithParameters_ShouldReturnLogInView()
LogIn_WithoutParameters_WhenAuthenticationFailed_ShouldReturnLogInView()
LogIn_WithoutParameters_WhenAuthenticationPassed_ShouldReturnProfileRedirect()

2.
LogIn_Get_ShouldReturnLogInView()
LogIn_Post_WhenAuthenticationFailed_ShouldReturnLogInView()
LogIn_Post_WhenAuthenticationPassed_ShouldReturnProfileRedirect()

3.
LogIn_ShouldReturnLogInView()
LogIn_WhenCalledWithParametersAndAuthenticationFailed_ShouldReturnLogInView()
LogIn_WhenCalledWithParametersAndAuthenticationPassed_ShouldReturnProfileRedirect()

有什么意见吗?

I am working on an asp.net mvc application and writing my unit tests BDD style.
Eg.

GetResource_WhenResourceFileExists_ShouldReturnResources()

But when I am writing tests for my controllers, I usually have two Methods with the same name. One without parameters for get requests and one with for posts. Does anybody have a good naming convention here to distinguish between the two?

I can think of:

1.
LogIn_WithParameters_ShouldReturnLogInView()
LogIn_WithoutParameters_WhenAuthenticationFailed_ShouldReturnLogInView()
LogIn_WithoutParameters_WhenAuthenticationPassed_ShouldReturnProfileRedirect()

2.
LogIn_Get_ShouldReturnLogInView()
LogIn_Post_WhenAuthenticationFailed_ShouldReturnLogInView()
LogIn_Post_WhenAuthenticationPassed_ShouldReturnProfileRedirect()

3.
LogIn_ShouldReturnLogInView()
LogIn_WhenCalledWithParametersAndAuthenticationFailed_ShouldReturnLogInView()
LogIn_WhenCalledWithParametersAndAuthenticationPassed_ShouldReturnProfileRedirect()

Any opinions?

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

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

发布评论

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

评论(5

时光瘦了 2024-08-13 09:21:54

我不太喜欢的一种选择是为控制器操作指定不同的名称,然后使用 ActionName 属性重命名它们:

public ActionResult Login() {
    // ... code ...
    return View();
}

[HttpPost]
[ActionName("Login")]
public ActionResult LoginPost(... some params ...) {
    // ... more code ...
    return View();
}

这本质上是用一个问题(单元测试命名)替换另一个问题(更难读取控制器代码) 。尽管如此,您可能会发现这种模式很有吸引力,因为它确实解决了上述问题。

One option, which I don't particularly like, is to give the controller actions different names, but to then rename them using the ActionName attribute:

public ActionResult Login() {
    // ... code ...
    return View();
}

[HttpPost]
[ActionName("Login")]
public ActionResult LoginPost(... some params ...) {
    // ... more code ...
    return View();
}

This essentially substitutes one problem (unit test naming) with another problem (harder to read controller code). Nevertheless, you might find this pattern appealing since it does solve the stated problem.

半衬遮猫 2024-08-13 09:21:54

我使用与您问题中的命名约定类似的命名约定,即 method_scenario_expected
我认为你应该详细说明“场景”部分 - 如果你传递参数 - 让读者知道它们有什么特别之处。

请记住,以这种方式命名您的测试更多的是“TDD 导向”,而不是 BDD - BDD 测试名称应该与规则和“行为有关:”。

如果您觉得当前的命名约定不影响代码的可读性 - 请随意尝试四处寻找适合您的方法。

I use a similar naming convention to the one in your question i.e. method_scenario_expected
I think you should elaborate more on the "scenario" part - if you're passing parameters - let the reader know what is speacial about them.

Keep in mind that naming your tests this way is more "TDD oreinted" and no BDD - BDD tests names should be about rules and "behaviors:.

If you feel that the current naming convention does not hwlp the code readability - feel free to experiment around and find what works for you.

给不了的爱 2024-08-13 09:21:54

我认为这是一个完美的例子,说明了为什么单元测试的严格命名约定没有吸引力。

您提出的方案仅在有两种方法重载时才有效:一种带参数,另一种不带参数。它不会扩展到具有多个具有不同参数的重载的情况。

就我个人而言,我更喜欢更宽松的命名约定,可以概括为:

[Action][Will|Should|Is|...][Result]

这使我可以灵活地命名我的测试,

SutIsPathResolutionCommand
ExecuteWithNullEvaluationContextWillThrow
ExecuteWillAddDefaultClaimsTransformationServiceWhenNoConnectionServiceIsAvailable

我必须承认我很少阅读测试的名称。相反,我阅读了它的作用的规范(即测试代码)。名字对我来说并不那么重要。

I think this is a perfect example of why rigid naming conventions for unit tests are unattractive.

Your proposed scheme will only work when you have two method overloads: one with and one without parameters. It doesn't extend to the scenario where you have more than one overload with different parameters.

Personally I prefer a much looser naming convention that can be summarized as

[Action][Will|Should|Is|...][Result]

This gives me the flexibility to name my tests

SutIsPathResolutionCommand
ExecuteWithNullEvaluationContextWillThrow
ExecuteWillAddDefaultClaimsTransformationServiceWhenNoConnectionServiceIsAvailable

I must admit that I rarely read the name of the test anyway. Instead, I read the specification of what it does (i.e. the test code). The name is just not that important to me.

财迷小姐 2024-08-13 09:21:54

我可能不会回答你的问题,但我想分享我所做的事情。
我不遵循特定的命名约定,但我尝试给出名称来解释测试方法试图测试的内容。在某些情况下,我需要更多解释,我会添加描述 [Test(“此测试评估特定用户回答了多少问题”)]。

需要确保的一件事是测试更具可读性并且易于理解。

I may not be answering your question, but I want to share what I do.
I don't follow specific naming convention, but I try to give names which explains what test method is trying to test. Some cases where I need more explanation I add description [Test("This test evaluates how many questions were answered by specific user")].

One thing to make sure is the tests are more readable and quickly understandable.

留蓝 2024-08-13 09:21:53

我使用以下格式,这对我来说非常有效:

[TestFixture]    
public class Log_in_with_parameters_should
{
    [Test]
    public void Return_the_log_in_view() {}
}

[TestFixture]    
public class Log_in_without_parameters_should
{
    [Test]
    public void Return_the_log_in_view_when_the_authentication_failed() {}

    [Test]
    public void Redirect_to_the_profile_when_the_authentication_passed() {}
}

I use the following format which works very well for me:

[TestFixture]    
public class Log_in_with_parameters_should
{
    [Test]
    public void Return_the_log_in_view() {}
}

[TestFixture]    
public class Log_in_without_parameters_should
{
    [Test]
    public void Return_the_log_in_view_when_the_authentication_failed() {}

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