-5

I am using NUnit to test a DLL developed by another group but have errors when trying to call a method in this external DLL which was set as reference in my test project. The error is: 'UT' is a namespace but is used like a type. I tried several ways but no one is working. How can I reference it correctly?

The method I try with this one worked with another class I developed, when the latter was in the same namespace. Now, with the external DLL, it simply shows error before the compile.

This is my procedure:

using NUnit.Framework;

namespace UnitTesting.GettingStarted.Tests
{

    [TestFixture]
    public class TestUT
     {
        [TestCase("A12345", "CII", "10000")]

        public void TestAccessVerification(string psCodeUsager, string psCodeApp, string psCodeFonction)
            {
                UT systemUnderTest = new UT();
                Assert.IsTrue(systemUnderTest.VerifierAcces(psCodeUsager, psCodeApp, psCodeFonction));
            }
     }
}

The source code in the DLL is something like this:

using ...;

namespace GZM
{
    public class UT
    {
        public static bool VerifierAcces(string psCodeUsager, string psCodeApp, string psCodeFonction)
        {
            ... // returns true or false
        {
    {        
{

The error happens at the line:

UT systemUnderTest = new UT();

where both 'UT' are underlined with an error 'UT' is a namespace but is used like a type.

But, if I go with:

var systemUnderTest = new GZM.UT();

the error will happen in the next line and

systemUnderTest.VerifierAcces

will be underlined with the message "Member 'UT.VerifierAcces(string, string, string) cannot be accessed with an instance reference; qualify it with a type name instead."

Normally, my test should work and return True, but I cannot even start it because of the errors in the calling procedure.

MikeJ
  • 1
  • 2
  • The answer to the question you suspect as a duplicate does not fix my problem as there is another error concerning the reference, although the dll is correctly referenced in the project. – MikeJ Apr 18 '19 at 16:39

2 Answers2

2

VerifierAccesis a static method, you can't call it from a concrete object instance.

Use

Assert.IsTrue(UT.VerifierAcces(psCodeUsager, psCodeApp, psCodeFonction));

instead.

Lennart
  • 9,657
  • 16
  • 68
  • 84
  • WIth this modification, I had an error "The type or namespace 'VerifierAcces' does not exist in the namespace 'UT'." So, I changed it to `Assert.IsTrue(GZM.UT.VerifierAcces(psCodeUsager, psCodeApp, psCodeFonction));` but the test gives the following message: "Message: System.IO.FileNotFoundException : Could not load file or assembly 'UT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.". As the DLL is correctly referenced, what am I missing? – MikeJ Apr 18 '19 at 15:46
0

You can't access a static method for a class that is instantiated. Either make the entire class static if you don't need it as an object, or make a non-static override for method VerifierAcces.

See Lennart's answer for a third option that probably makes more sense for your use-case, assuming you need UT to be a concrete object.

Luke
  • 743
  • 6
  • 20