Question: I am trying to improve my unit testing skills and i was wondering if someone could tell me the best way of writing a test for writing a test for "sorted odd numbers of a given array of integers". At the moment I am passing the data into the test with params but I would to verify the output is correct aswell.

```
    [Theory]
    [InlineData(1,2,3,4)]
    public void SortArrayOddNumber(params int[] number)
    {
        var s = number.Where(n => (n % 2) == 0).OrderBy(n=>n);
    }

```

Answer: 
You should pass your input array and expected array as parameters in your tests .

You can use InlineData like this :

```
    [Theory]
    [InlineData(new[] { 0, 1, 2, 3, 4 }, new[] { 0, 2, 4 })]
    [InlineData(new[] { 0, 1, 3, 4 }, new[] { 0, 4 })]
    public void Should_return_odd_numbers_in_ascending_order(int[] input, int[] expectedSortedArray)
    {
        var result = input.SortOddNumbers();

        result.Should().BeEquivalentTo(expectedSortedArray, opt => opt.WithStrictOrdering());
    }

```

Or you can use MemberData like this :

```
    public static IEnumerable<object[]> Data => new List<object[]>
    {
       new object[] {new[] {0, 1, 2, 3, 4}, new[] {0, 2, 4}},
       new object[] {new[] {0, 1, 3, 4}, new[] {0, 4}}
    };

    [Theory, MemberData(nameof(Data))]
    public void Should_return_odd_numbers_in_ascending_order(int[] input, int[] expectedSortedArray)
    {
        var result = input.SortOddNumbers();

        result.Should().BeEquivalentTo(expectedSortedArray, opt => opt.WithStrictOrdering());
    }

```

And SortOddNumbers Extension method :

```
public static class NumberExtensions
{
    public static int[] SortOddNumbers(this int[] input)
    {
        return input.Where(i => (i % 2) == 0).OrderBy(i => i).ToArray();
    }
}

```
You need to pass as a parameter the expected output. From the most basic point of view the unit test should be like:

* input data, preferably all parameters required by tye method to be tested,
* name of the unit test method shoul be descriptive and tell exacly what it is cheking,
* should compare result from tested method's return with the expected value.

Example:

Some class in the project - let assume **MyLib** library project:

**Sort.cs**

```
using System.Linq;

namespace MyLib 
{
    public class Sort
    {
        public int[] SortArrayOddNumber(int[] arrayToSort)
        {
            return arrayToSort
                .Where(n => (n % 2) == 0)
                .OrderBy(n => n)
                .ToArray();
        }
    }
}

```

Test project **MyLib.Tests**:

**SortTest.cs**

```
using Xunit;

namespace MyLib.Tests
{
    public class SortTest
    {
        // System under test
        private readonly Sort sut;

        public SortTest()
        {
            sut = new Sort();
        }

        [Theory]
        [InlineData(new[] { 1, 2, 3, 4}, new[] { 2, 4 })]
        public void SortArrayOddNumber_should_return_array_with_only_even_numbers_sorded_inc(
            int[] arrayToSort,
            int[] expectedResult)
        {
            // Arrange
            // well... nothing to do before running our method we are testing...

            // Act
            var result = sut.SortArrayOddNumber(arrayToSort);

            // Assert
            Assert.Equal(expected: expectedResult, actual: result);
        }
    }
}

```

[![enter image description here](https://i.stack.imgur.com/JwfUI.png)](https://i.stack.imgur.com/JwfUI.png)
[![enter image description here](https://i.stack.imgur.com/qhtMR.png)](https://i.stack.imgur.com/qhtMR.png)

So:

* we have our method to be tested,
* we create **\*.Tests** projekcts for each of out projects to be tested (**A** - **A.Tests**),
* we create for each class within project a class within test project (**Sort.cs** - **SortTest.cs**)
* we add a reference to the project
* in the costructor wee create a class we will be testing,
* we define all requirements for the code that we want it and we just write them in a form of methods,
* methods are running the code to check the given property of our method.

And serveral remarks at the end:

* there are multiple ways of naming methods in testing. In general I spent a lot of time trying to pick the best one but I would say that it should meet only 1 condition: `reading the name should get You an answer what does this method check`. And maybe second: `stick to the selected convention`
* You should **never** calculate expected result. The chances are preety the same that You will make a mistake there or in the codee You are testing. You should know in advance what are the inputs and what You want to get from the method. So You shouldn't check the result by recalculating it but in a different, "safer" way.