Are multiple statements wrong in this test?

I am trying to test the Login method, which, when the user and password parameters are correct, sets the value of two session variables and three cookies and finally returns true.

I read a few posts about unit testing, but for some reason this didn't completely help me. I know that there should be only one statement per unit test, although you can use more than one while you test one “logical concept”.

The login method is correct only if it correctly sets each session variable and cookie and returns the expected value, so I'm not sure it would be good to check all these values ​​at once (this will result in using six statements in the unit test, I think it's a little dirty) , or if I have to check the value of each session variable and cookie separately in different tests.

[TestMethod()]
public void SuccessfulLoginTest()
{
    // Arrange.
    String username = "foo";
    String password = "correct password";
    Boolean remember = true;

    // Act.
    Boolean actual = Login(username, password, remember);

    // Assert.
    Assert.IsTrue(actual);
    Assert.AreEqual("foo", HttpContext.Current.Session["Username"]);
    Assert.AreEqual(1, HttpContext.Current.Session["Group"]);
    Assert.AreEqual("foo", HttpContext.Current.Response.Cookies["Username"].Value);
    Assert.AreEqual("en", HttpContext.Current.Response.Cookies["Lang1"].Value);
    Assert.AreEqual("es", HttpContext.Current.Response.Cookies["Lang2"].Value);
}
+4
source share
4 answers

. , , Assert unit test. " ", . , , . ( ) .

"", .

+3

, , Assert. , , , , . :

void AssertSessionIsValid(string username, int group, ...)
{
    Assert.AreEqual(username, HttpContext.Current.Session["Username"]);
    Assert.AreEqual(group, HttpContext.Current.Session["Group"]);
    ...
}

, , Shouldly.

+3

, , "" . , ; , , .

+1

Are your conditions consistent in the boolean, and if everything is satisfied, then the boolean should return true.

Asset this boolean variable

bool AreAllConditionsFulfilled = condition1 && condtionCheckingSessionVariablesOne && conditionCheckingSessionVariablesTwo

I would also suggest that separate test cases test each of the session variables and state that they are correct.

[Test]
public void TestUserNameSessionVariable()
{
//Login Code
Assert.AreEqual("foo", HttpContext.Current.Session["Username"]);
}
-1
source

All Articles