Naming Conventions for ASP.NET MVC RTM

I am working on an asp.net mvc application and writing down my unit tests BDD. For example.

GetResource_WhenResourceFileExists_ShouldReturnResources ()

But when I write tests for my controllers, I usually have two methods with the same name. One without parameters for receiving requests and one for messages. Does anyone 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?

+7
unit-testing tdd asp.net-mvc testing bdd
source share
5 answers

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() {} } 
+3
source share

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

Your proposed scheme will work only if there are two method overloads: one with one and without parameters. It does not apply to a scenario in which you have more than one overload with different parameters.

Personally, I prefer a much weaker naming convention, which can be summarized as

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

It gives me the opportunity 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. test code). The name is not so important for me.

+1
source share

One option that I particularly dislike is to give the controller different names, but 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 replaces one problem (unit test naming) with another problem (it is more difficult to read the controller code). However, you may find this template attractive as it solves the indicated problem.

+1
source share

I use a similar naming convention with the one that was in your question, that is, method_scenario_expected I think you should better understand the part of the "script" - if you are passing parameters, let the reader know what is special about them.

Keep in mind that naming your tests in this way is more "TDD reinted", and BDD-BDD test names should not be about rules and "behavior :.

If you think that the existing naming convention does not support code readability, feel free to experiment and find what works for you.

+1
source share

Perhaps I do not answer your question, but I want to share what I am doing. I do not adhere to a specific naming convention, but I am trying to give names that explain what the test method is trying to verify. In some cases, when I need more explanation, I add a description [Test ("This test measures how many questions a particular user answered")].

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

0
source share

All Articles