0

I have a simple function that takes Client and Supplier data and then calculates tax based on where they live and other information:

static int CountTax(Supplier tiek, Client kl, bool same_country)
{
    if ( !tiek.is_PVM_payer)
        return 0;
    else
    {
        if (!kl.EU)
            return 0;
        else
        {
            if (same_country)
                return kl.x;
            else
            {
                if (kl.is_PVM_payer)
                    return 0;
                else
                    return kl.x;
            }
        }
    }
}

Now I am required to write tests for this function. It is the first time I am encountering tests. I am using XUnit testing. And my test class looks like this:

using System;
using Xunit;

namespace CountTax_tests
{
    public class UnitTest1
    {

        [Theory]
        [InlineData(typeof(Client))]
        public void Tax_is_0()
        {
            // arrange
            int expectedVal = 0;
            int actualVal;

            /// act
            actualVal = MyProgram.MyprogramCode.CountTax(supplier, client, same_country);

            // assert
            Assert.Equal(expectedVal, actualVal);
        }

        [Fact]
        public void PVM_is_x()
        {
            int expectedVal = x;
            int actualVal;

            actualVal = MyProgram.MyprogramCode.CountTax(supplier, client, same_country);
            Assert.Equal(expectedVal, actualVal);

        }
    }
}

But how do I pass my Client and Supplier parameters to that test? Please help me and lead on a path because I am completely new and nothing is clear to me even after many tutorials... Maybe I have to use [Theory]? Or maybe [Fact]? Or maybe it is impossible to pass classes as parameters? Also, [InlineData(typeof(Client))] is being underlined in red and is not working.

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
vytaute
  • 1,260
  • 4
  • 16
  • 36
  • Pass it as method arguments, there is an existing [thread](https://stackoverflow.com/questions/22093843/pass-complex-parameters-to-theory) – Pavel Anikhouski Oct 25 '19 at 17:33
  • 2
    This is unrelated to the question itself, but you don't need an `else` after a return statement, as the only scenario in which anything after the return would be reached is when the `if` condition is not met. You can make your code much prettier by eliminating all those unnecessary `else` blocks. – Inagnikai Oct 25 '19 at 18:39
  • Why do you feel the need to pass in your class as a parameter in your unit test? Why not just instantiate the object within your unit test itself and just pass it to your static method? Unit tests just care about making sure your functionality works as intended so all you need to do is just make sure the object exists in the way that makes the most sense in context. IOC is not as much a concern here. – Paul Carlton Oct 26 '19 at 00:47

0 Answers0