1

In the code below, I have the "howmanystringasync" method which is calling two others methods. Those two are faked. The second fake's return does not work because of the ".ToList()".

I usually returns IEnumerable because in some case I want to restrict the caller actions. And sometimes, I ask in input directly a List so a method can do what a List has to offer.

How can I make the test below works ?

var result = await f.AccessTheWebAsync2(web.ToList());

using FakeItEasy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace api.tests.unit
{
    public class SpecialString
    {
        public int IntProperty { get; set; }
        public string StringProperty { get; set; }

        public override bool Equals(object obj)
        {

            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            if (ReferenceEquals(this, obj)) return true;

            return Equals(obj as SpecialString);
        }

        public bool Equals(SpecialString other)
        {
            if (other == null) return false;

            return (this.IntProperty == other.IntProperty) && (this.StringProperty == other.StringProperty);
        }
    }

    public interface IFoo
    {
        Task<IEnumerable<SpecialString>> AccessTheWebAsync(int a, int b);
        Task<int> AccessTheWebAsync2(List<SpecialString> integers);
    }

    public class Foo : IFoo
    {
        public async Task<IEnumerable<SpecialString>> AccessTheWebAsync(int a, int b)
        {
            HttpClient client = new HttpClient();
            string result = await client.GetStringAsync("https://msdn.microsoft.com");

            var results = new List<SpecialString> {
                        new SpecialString{
                            IntProperty = 1,
                            StringProperty = "stringprop1"
                        },
                    new SpecialString{
                        IntProperty =2,
                        StringProperty = "stringprop2"
                    }
                };

            return results;
        }

        public async Task<int> AccessTheWebAsync2(List<SpecialString> specialstrings)
        {
            HttpClient client = new HttpClient();
            string result = await client.GetStringAsync("https://msdn.microsoft.com");

            var results = new List<SpecialString> {
                        new SpecialString{
                            IntProperty = 1,
                            StringProperty = "stringprop1"
                        },
                    new SpecialString{
                        IntProperty =2,
                        StringProperty = "stringprop2"
                    }
                };

            return results.Count();
        }

    }

    public class BiggerFoo
    {
        private readonly IFoo f;

        public BiggerFoo(IFoo f)
        {
            this.f = f;
        }

        public async Task<int> howManyStringsAsync(int a, int b)
        {

            var web = await f.AccessTheWebAsync(a, b);

            var result = await f.AccessTheWebAsync2(web.ToList());
            return result;
        }
    }

    public class FooTest
    {
        [Fact]
        public void testasyncmethod()
        {
            var fakeFoo = A.Fake<IFoo>();
            IEnumerable<SpecialString> result = new List<SpecialString> {
                        new SpecialString{
                            IntProperty = 1,
                            StringProperty = "fakestringprop1"
                        },
                    new SpecialString{
                        IntProperty =2,
                        StringProperty = "fakestringprop2"
                    }
                };

            A.CallTo(() => fakeFoo.AccessTheWebAsync(1, 2)).Returns<Task<IEnumerable<SpecialString>>>(Task.FromResult(result));

            A.CallTo(() => fakeFoo.AccessTheWebAsync2(result.ToList())).Returns<Task<int>>(Task.FromResult(5));

            var bFoo = new BiggerFoo(fakeFoo);
            bFoo.howManyStringsAsync(1, 2);
        }
    }

}
olleo
  • 378
  • 3
  • 14

1 Answers1

2

If I read things right, the problem is that you configure AccessTheWebAsync2 to return 5 when given a particular List<SpecialString>, but in your test, the method is called with a different List<SpecialString>, and List<SpecialString>.Equals only does reference equality, so you're getting 0 back. If you want to make sure 5 is returned when a List<SpecialString> containing your desired SpecialStrings is passed to AccessTheWebAsync2, you'll need to adjust the constraint. It looks like SpecialString doesn't have a value-based Equals either, so you could consider examining the elements' properties:

A.CallTo(() => fakeFoo.AccessTheWebAsync(1, 2)).Returns(result);
A.CallTo(() => fakeFoo.AccessTheWebAsync2(
        A<List<SpecialString>>.That.Matches(l =>
            l.Count == 2 && l[0].IntProperty == 1 && l[1].StringProperty == "fakestringprop2")))
 .Returns(5);

Or, if you really don't care about the input value, something like

A.CallTo(() => fakeFoo.AccessTheWebAsync2(A<List<SpecialString>>._))
 .Returns(5);

Update: now that you've added SpecialString.Equals, using the list's values as a call matching constraint is easier:

A.CallTo(() => fakeFoo.AccessTheWebAsync2(
     A<List<SpecialString>>.That.IsSameSequenceAs(result)))
  .Returns(5);

If you haven't already, do check out all of the argument constraints that FakeItEasy provides.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • Updated the code with Equals. I feel that if I used the "ignore" feature of FakeItEasy, I'd be missing a free test : the one that state that the faked method is called with the right parameter given the caller's input. Perhaps I should ignore every parameters in every faked methods in general ? – olleo Dec 22 '18 at 08:00
  • It's hard to say. I take it on a case-by-case basis. It really depends on each use case and what benefit you feel the stronger assertions bring, when compared with the brittleness and readability of the tests. – Blair Conrad Dec 22 '18 at 12:10
  • Updated answer to leverage your new `Equals` method. – Blair Conrad Dec 22 '18 at 12:19