1

I'm trying to make a simple test so I can practice. I don't know what I'm doing wrong, but I get

The name 'Program' does not exist in the current context [/dir/unitTestTest/unitTestTest.csproj]

And I don't know why. Here is my code.

I have a Program.cs

using System;

namespace unitTestDotNet
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int yy = Add(2,1);

            Console.WriteLine(yy);
            Console.WriteLine(IsOdd(5));
        }

        public static int Add(int x, int y) 
        {
            return x + y;
        }

        public static bool IsOdd(int value) 
        {
            return value % 2 == 1;
        }
    }
}

testclass.cs

using Xunit;

public class testclass 
{
    [Fact]
    public void PassingAddTest()
    {
        Assert.Equal(4, Program.Add(2,2));
    }

}

And in the unitTestProject.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1"/>
    <PackageReference Include="xunit" Version="2.4.1"/>
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"/>
  </ItemGroup>
</Project>

I'm new to ASP.net core and I don't know what I'm doing wrong

daniel gon
  • 159
  • 2
  • 14
  • Are the test and the program in the same project? – Jonathon Chase Mar 22 '19 at 16:58
  • This is basic C# - you have to have a reference to the project, and you have to either bring the namespace into scope via a using statement (`using unitTestDotNet;` or using a fully qualified name (`unitTestDotNet.Program.Add(2,2)`). And your class needs to not be private. – mason Mar 22 '19 at 16:58
  • Yes, there are in the same project – daniel gon Mar 22 '19 at 16:59
  • Are you *sure* they're in the same project? Because it looks to me like you have a console project, and a unit test project. They might be in the same solution, but they're likely not the same project. If your unit test project is going to use code from your console project, it needs to have a reference to your console project. – mason Mar 22 '19 at 17:01
  • Also, looks like your test is outsite the same namespace of your Program. – Vagner Lucas Mar 22 '19 at 17:01
  • Yes, I put the class testclase inside the namespace unitTestDotNet and it worked. – daniel gon Mar 22 '19 at 17:03
  • Did it work even with internal visibility? – Vagner Lucas Mar 22 '19 at 17:05
  • Yes, I didn't change it – daniel gon Mar 22 '19 at 17:06

3 Answers3

0

The Program class should be declared as public if you want to access it in another class. All classes default to internal, while members default to private.

Gustavo Passos
  • 84
  • 1
  • 10
  • Classes default to `internal`, not `private`. Class members do default to `private`. – Jonathon Chase Mar 22 '19 at 17:03
  • You're right, actually it's not possible to declare a type member of an namespace as private: https://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c. – Gustavo Passos Mar 22 '19 at 17:06
0

Change your Program class visibility to public and be sure you have correct project reference from UnitTest-project to project where public class Program is.

Risto M
  • 2,919
  • 1
  • 14
  • 27
0

Although the answers about the visibility are correct when testing, in your case the testclass was outside the same namespace as your Program.

Take a look at https://visualstudiomagazine.com/articles/2018/11/01/xunit-tests-in-net-core.aspx to learn more about testing and good practices.

Vagner Lucas
  • 477
  • 1
  • 4
  • 8