vendredi 25 octobre 2019

How to test function that has objects as parameters in XUnit?

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.

Aucun commentaire:

Enregistrer un commentaire