Alexandra K Alexandra K - 3 months ago 11
C# Question

The Unit Test does not see my methods

I created a unit Test project for my CalculatorApp and added the app as a reference for my UniTest Project

My code looks like this:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CalculatorApp;


namespace CalculatorUnitTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestAddMethod()
{
decimal firstTestNumber = 7;
decimal secondTestNumber = 3;
decimal resultTest = 10;


Assert.AreEqual(resultTest, AddMethod(firstTestNumber, secondTestNumber));

}
}


But it says that AddMethod does not exist in the current context.

What am I missing? (i don't have much experience with unitTests)
I am using Visual Studio Community 2015.

Answer

As Jeroen already mentioned, you need an instance of the calculator class you want to test. Try editing your test in a way like this:

[TestMethod]
public void TestAddMethod()
{
    decimal firstTestNumber = 7;
    decimal secondTestNumber = 3;
    decimal expected = 10;

    // create an instance of your system under test (sut)
    var sut = new CalculatorApp.Calculator();

    // invoke the method, you want to test and keep the actual result
    var actual = sut.AddMethod(firstTestNumber, secondTestNumber)

    Assert.AreEqual(expected, actual);
}
Comments