FakeItEasy返回错误的返回

时间:2018-12-22 02:01:17

标签: c# fakeiteasy

在下面的代码中,我有一个“ howmanystringasync”方法,该方法正在调用另外两个方法。那两个是假的。 由于出现“ .ToList()”,第二次伪造的退货不起作用。

我通常返回IEnumerable,因为在某些情况下我想限制调用者的操作。有时,我会在输入中直接询问一个列表,以便某个方法可以执行列表必须提供的功能。

如何使以下测试有效?

var结果=等待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);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

如果我没看错的话,问题是您将AccessTheWebAsync2配置为在给定特定的List<SpecialString>时返回5,但是在您的测试中,该方法用另一个List<SpecialString>调用,而List<SpecialString>.Equals仅引用相等,因此您将获得0。如果要确保将包含所需List<SpecialString>的{​​{1}}传递给SpecialString时返回5,则需要调整约束。看来AccessTheWebAsync2也没有基于值的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

如果还没有,请签出all of the argument constraints that FakeItEasy provides