0

I have two projects, BasicCalculator and BasicCalculator.Test.Unit. I am wanting to test classes with certain functions from the BasicCalculator project. I created a unit test file named BasicCalculator.Test.Unit and added a new reference (BasicCalculator) inside of that project.

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

namespace BasicCalculator.Test.Unit
{
    [TestClass]
    public class CalculatorEngineTest
    {
        private readonly CalculatorEngine _calculatorEngine = new CalculatorEngine();
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}

However, whenever I type private readonly CalculatorEngine _calculatorEngine = new CalculatorEngine(); it displays an error for CalculatorEngine which is a class in the BasicCalculator project.

It says:

class BasicCalculator.CalculatorEngine

BasicCalculator - Available
BasicCalculator.Test.Unit - Not Available

You can use the navigation bar to switch context.

The type or namespace 'CalculatorEngine' could not be found (are you missing a using directive or an assembly reference?)

Am not sure why this is occurring if I just needed to add a reference in the BasicCalculator.Test.Unit project that calls the BasicCalculator project.

ADDED INFORMATION

This is my class that I am trying to access inside of BasicCalculator.Test.Unit

using System;
using System.Collections.Generic;
using System.Text;

namespace BasicCalculator
{
    public class CalculatorEngine
    {
        public double Calculate(string argOperation, double argFirstNumber, double argSecondNumber)
        {


        }
    }
}

Solution Explorer View

Error List

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • In both `BasicCaclualtor.Test.Unit - Not Available` and `CalcualtorEngine`, Calculator is spelled terribly wrong. This is pretty likely to be the source of your problem. – Klaycon Jan 02 '20 at 22:21
  • `CalcualtorEngine` != `CalculatorEngine` –  Jan 02 '20 at 22:21
  • I am sorry, I was re-typing the error message. But this is not the problem. –  Jan 02 '20 at 22:26
  • can you show us the basiccalculator.CalculatorEngine class? i suspect it may be the access modifier there – Muckeypuck Jan 02 '20 at 22:27
  • 1
    It's a different namespace, right? Don't you need `using BasicCalculator;` at the top of your test class file? – Klaycon Jan 02 '20 at 22:28
  • Please see updated post @Klaycon –  Jan 02 '20 at 22:32

2 Answers2

1

Simply adding the project as a reference doesn't expose the project's public members to all classes in your code. You need to tell the IDE you want to link the project and use its classes with a using statement:

using BasicCalculator;

Looking at the edited question, it seems CalculatorEngine has no access modifier, meaning it has protected visibility. Classes outside of the BasicCalculator project won't be able to access it. You'll also need to make it public:

public class CalculatorEngine

Edit: The error list in the updated question reveals an error:

Project '..\BasicCalculator.csproj' targets 'netcoreapp3.1'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.7.2'

This error is clearly stopping the BasicCalculator reference from resolving in your test project. You'll need to change the framework target in the project settings in one or the other so that they match. Or, research this error for other possible resolutions - here is a relevant SO question.

Klaycon
  • 10,599
  • 18
  • 35
  • Doing this, still returns the same message. –  Jan 02 '20 at 22:35
  • According to the documentation if I am interpreting it correctly. (https://learn.microsoft.com/en-gb/dotnet/standard/net-standard) For BasicCalculator, I went to properties and changed the .net core to 2.0 and then rebuilt it (it was a success) then I changed the properties .net framework to 4.6.1 and rebuilt and and now I get the same error as above but this time for .netcoreapp2.0 and .NetFrameWork,Version=v4.6.1. Are these still not compatible? I looked at that chart on that link. –  Jan 02 '20 at 23:21
  • @simpleCoder I think you're misreading the table. The table specified which version the **.NET Standard** framework can act as a sort of interop between. Perhaps you have to make one of the projects target .NET Standard instead of .NETFramework or .NET Core. – Klaycon Jan 02 '20 at 23:24
  • Thank you for the clarification, I went ahead and created the first project as a .NETFramework and for the second one I used a .NETFramework and now I am able to use that class from the first project. Thanks again for your help! –  Jan 03 '20 at 02:16
1

class CalculatorEngine should be public class CalculatorEngine to expose it to callers outside the project its declared in

you should also add using BasicCalculator; as @klaycon stated in his answer

Muckeypuck
  • 549
  • 2
  • 14
  • I modified `class CalculatorEngine` to `public class CalculatorEngine` inside of the CalculatorEngine.cs file and then checked the `CalculatorEngineTest.cs` file and I still am getting the same message as before. –  Jan 02 '20 at 22:37
  • did you add the using statement per klaycons answer? – Muckeypuck Jan 02 '20 at 22:37
  • 1
    @simpleCoder make sure you recompile the project after changing the access modifier – Klaycon Jan 02 '20 at 22:37
  • Yes I did. @Muckeypuck –  Jan 02 '20 at 22:38
  • I did this as well. @Klaycon and still am getting the same message. –  Jan 02 '20 at 22:39
  • Youre sure you added the reference to BasicCalculator inside the test project? – Muckeypuck Jan 02 '20 at 22:40
  • @simpleCoder Does the line `using BasicCalculator;` error? If you write `BasicCalculator.` (with a dot) where the compiler would expect a type (class member, variable declaration) does IntelliSense bring up any class suggestions? Has the error message changed at all? – Klaycon Jan 02 '20 at 22:49
  • @Klaycon Please see my updated code (1st and 3rd block of code). That is how I have it set up. Even with your suggestions, I am getting the same errors. –  Jan 02 '20 at 22:50
  • @simpleCoder I know. I ask those questions because they are debugging steps that would help narrow down possible issues. – Klaycon Jan 02 '20 at 22:51
  • @Klaycon I added a picture of the errors I am getting. –  Jan 02 '20 at 22:52
  • BasicCalculator.test.unit wont build until BasicCalculator does. Whats test class? – Muckeypuck Jan 02 '20 at 22:56
  • @Muckeypuck BasicCalculator builds and runs perfectly until I add a new project (BasicCalculator.test.unit) within the BasicProject folder. –  Jan 02 '20 at 23:05
  • So I did the following: I removed the new project (BasicCalculator.Test.Unit) and created a new one. But this time instead of placing the new project inside of the BasicCalculator project, I placed it outside of the folder. And now when I added the reference and used `using BasicCalculator;` and build the new project. it returns this: `Severity Code Description Project File Line Suppression State Error Project '..\BasicCalculator\BasicCalculator.csproj' targets 'netcoreapp3.1'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.7.2'. BasicCalculator.Test.Unit` –  Jan 02 '20 at 23:10