Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid signature for SetUp or TearDown method - What am I doing wrong?

I am trying to do some dependency injection for my tests using nUnit. I'm new to TDD and nUnit so it's possible I am missing something simple. So basically I've created a SetUp method for my interfaces. I originally was using a constructor but I read it's bad to do this when doing TDD so I now using a method.

When I run my test I construct an object and assign it to the interface and then I call a method using that interface. I want to test if it can parse a string decimal.

When I run my test it says test failed and the message is:Invalid signature for SetUp or TearDown method

See below for the actual code:

 public class DonorTests
    {
        private IDonor _Donor;
        private IValidateInput _ValidInput;


        //DonorTests(IDonor donor, IValidateInput validInput)
        //{
        //    _Donor = donor;
        //    _ValidInput = validInput;
        //}

        [SetUp]
        void Setup(IDonor donor, IValidateInput validInput)
        {
            _Donor = donor;
            _ValidInput = validInput;
        }

        [Test]
        public void HandleStringNotDecimal()
        {
            _ValidInput = new ValidateInput();
            Assert.IsTrue(_ValidInput.IsDecimal("3445.3450"));
        }
    }

My class that uses this interface

 public class ValidateInput : IValidateInput
    {
        public decimal RoundTwoDecimalPlaces(decimal amount)
        {
            return Math.Round(amount);
        }

        public bool IsDecimal(string amount)
        {
            decimal ParsedDecimal;
            return Decimal.TryParse(amount, out ParsedDecimal);
        }

        public decimal ConvertToString(string value)
        {
            decimal ParsedDecimal;
            Decimal.TryParse(value, out ParsedDecimal);
            return ParsedDecimal;
        }
    }
like image 582
nick gowdy Avatar asked Aug 23 '14 15:08

nick gowdy


2 Answers

You're injecting dependencies using constructor injection previously, right? I think you will not be able to perform dependency injection using method decorated with SetUpAttribute because such method has to be parameterless. Also Setup method has to be public, see this SO thread.

How are we typically dealing with similar situations in our company is:

[TestFixture]
public class DonorTests
{
    private IDonor _Donor;
    private IValidateInput _ValidInput;

    [SetUp]
    public void Setup()
    {
        _Donor = new Donor();
        _ValidInput = new ValidateInput();
    }

    [Test]
    public void HandleStringNotDecimal()
    {
        Assert.IsTrue(_ValidInput.IsDecimal("3445.3450"));
    }
}

Or if construction of ValidInput and Donor is cheap then we simply create new instance for each test, having special method for that purpose so when we decide to test another implementation of IValidateInput then it is enough to change it in one place only:

[TestFixture]
public class DonorTests
{
    [Test]
    public void HandleStringNotDecimal()
    {
        var validInput = CreateValidateInput();
        Assert.IsTrue(validInput .IsDecimal("3445.3450"));
    }

    private static IValidateInput CreateValidateInput()
    {
        return new ValidateInput();
    }
}
like image 55
Michal Hosala Avatar answered Nov 19 '22 05:11

Michal Hosala


Besides the cause mentioned in the accepted answer, I have met the same error when leaving method as non-public (private or protected).

NUnit most probably relies on reflection and does not deal with non-public methods, so special methods (i.e. decorated with NUnit specific attributes) must be public.

like image 28
Alexei - check Codidact Avatar answered Nov 19 '22 07:11

Alexei - check Codidact