FakeItEasy return打错了return

FakeItEasy returning the wrong return

在下面的代码中,我有 "howmanystringasync" 方法调用另外两个方法。这两个是假的 由于“.ToList()”.

,第二个假的 return 不起作用

我通常 returns IEnumerable 因为在某些情况下我想限制调用者的操作。有时,我直接要求输入一个 List,这样一个方法就可以完成 List 必须提供的功能。

我怎样才能使下面的测试有效?

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);
        }
    }

}

如果我没看错的话,问题是当给定一个特定的 List<SpecialString> 时,您将 AccessTheWebAsync2 配置为 return 5,但在您的测试中,该方法被调用不同的 List<SpecialString>List<SpecialString>.Equals 只做引用相等,所以你得到 0 回来。如果要确保在包含所需 SpecialStringList<SpecialString> 传递给 AccessTheWebAsync2 时 returned 5,则需要调整约束。看起来 SpecialString 也没有基于值的 Equals,因此您可以考虑检查元素的属性:

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);

或者,如果你真的不关心输入值,像

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

更新:现在您已经添加了SpecialString.Equals,使用列表的值作为调用匹配约束更容易:

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

如果您还没有,请查看 all of the argument constraints that FakeItEasy provides