0

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, "");

}
5
  • Also, what is the type of Client.AccountName? Are you sure it's string, and that the property is defined by the class? Commented Aug 31, 2016 at 19:29
  • Added the error and yes I am positive it is type string thanks to visual studio ;) Commented Aug 31, 2016 at 19:31
  • Why did my question get voted down? Commented Aug 31, 2016 at 20:13
  • First, I did not down vote your question. Second, This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. which usually leads to votes to close these as off topic. Commented Sep 1, 2016 at 2:12
  • 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 is a syntax error, string acctName which is for when you are defining a method, not trying to call it. Hope that helps. Commented Sep 1, 2016 at 2:16

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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);

3 Comments

Thanks for the answer and explanation @Nkosi but why is expectedKey in brackets in string actualValue = result[expectedKey]; ?
The assumption was that Dict was of type Dictionary which would allow an indexed call with the key that returns the value stored at that key.
I just learned something new. :) I thought an index call had to reference the index number to get the value, like [0] or [1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.