Reputation: 803
I am a junior dev that is new to unit testing. My company uses NUnit and I am trying to test a null check in a service method I created. Any idea what my Assert statement should look like if I am trying to test if string acctName = ""
? For some reason string acctName
is getting compiler error that says
"The name does not exist in the current context."
MY METHOD:
public Dict getOrder(Client client)
{
string acctName = client != null ? client.AccountName : "";
Dict replacements = new Replacement
{
{COMPANY_NAME, acctName}
};
return new Dict(replacements);
}
MY TEST:
public void getOrderNullTest()
{
//Arrange
Client myTestClient = null;
//Act
contentService.getOrder(myTestClient);
//Assert
Assert.AreEqual(string acctName, "");
}
Upvotes: 0
Views: 3683
Reputation: 247018
While you eventually answered your own question and got it to work, know that the issue was that in calling the assert you have Assert.AreEqual(string acctName, "")
which has a syntax error, string acctName
which is for when you are defining a method, not trying to call it.
Here is another way you could have written it
//Arrange
Client myTestClient = null;
string expectedValue = String.Empty;
string expectedKey = COMPANY_NAME;
//Act
Dict result = contentService.getOrder(myTestClient);
//Assert
Assert.IsNotNull(result);
string actualValue = result[expectedKey];
Assert.IsNotNull(actualValue);
Assert.AreEqual(expectedValue, actualValue);
Upvotes: 0
Reputation: 803
I ended up writing it like this:
//Arrange
Client myTestClient = null;
string expectedValue = String.Empty;
string expectedKey = COMPANY_NAME;
//Act
Dict actual = contentService.getOrder(myTestClient);
//Assert
Assert.IsTrue(actual.ContainsKey(expectedKey));
Assert.IsTrue(actual.ContainsValue(expectedValue));
Upvotes: 1