【问题标题】:Rhino Stub not working with Arg<object>.Is.EqualRhino Stub 不使用 Arg<object>.Is.Equal
【发布时间】:2016-05-05 16:49:43
【问题描述】:

我有一个以对象为参数的方法

方法签名

  IEnumerable<TResult> ExecuteSql<TResult>(string sql, object param)

方法调用

   string query ="select * from table";
   var data = _Executor.ExecuteSql<ObjectToReturn>(query,
                    new
                    {
                        CustomerID = customerId,
                        AnotherId1= id2,
                        AnotherId2 = id3
                    });

单元测试

使用下面的示例 1 模拟它不起作用(不返回存根返回对象。)

1

 _procExecutor.Stub(x => x.ExecuteSql<ObjectToReturn>(Arg<string>.Is.Anything,
                Arg<object>.Is.Equal(new
                {
                    CustomerID = customerId,
                    AnotherId1 = 10,
                    AnotherId2 = 11
                }))).Return(new List<ObjectToReturn>() { new ObjectToReturn() { id = 100 } });      

这有效(确实返回存根返回对象。)

2

_procExecutor.Stub(x => x.ExecuteSql<ObjectToReturn>(Arg<string>.Is.Anything,
                Arg<object>.Is.Anything)).Return(new List<ObjectToReturn>() { new ObjectToReturn() { id = 100 } });     

但我想确保将正确的参数传递给正确的字段,所以我像 #1 一样对我的方法进行存根,但它不返回存根返回对象。

我在示例 1 中做错了什么?

【问题讨论】:

    标签: c# unit-testing rhino-mocks


    【解决方案1】:

    问题是匿名参数,你可以像这个例子一样使用匹配:

    IUrlProvider urlProvider = MockRepository.GenerateStub<IUrlProvider>();
    
    urlProvider.Stub(u => u.Action(
        Arg<string>.Is.Equal("ValidateCode"),
        Arg<object>.Matches(new PropertiesMatchConstraint(new { code = "spam-and-eggs" })) ))
        .Return("");
    
    public class PropertiesMatchConstraint : AbstractConstraint
    {
        private readonly object _equal;
    
        public PropertiesMatchConstraint(object obj)
        {
            _equal = obj;
        }
    
        public override bool Eval(object obj)
        {
            if (obj == null)
            {
                return (_equal == null);
            }
            var equalType = _equal.GetType();
            var objType = obj.GetType();
            foreach (var property in equalType.GetProperties())
            {
                var otherProperty = objType.GetProperty(property.Name);
                if (otherProperty == null || property.GetValue(_equal, null) != otherProperty.GetValue(obj, null))
                {
                    return false;
                }
            }
            return true;
        }
    
        public override string Message
        {
            get
            {
                string str = _equal == null ? "null" : _equal.ToString();
                return "equal to " + str;
            }
        }
    }
    

    来源:

    Arg<object>.Is.Equal with anonymous objects

    【讨论】:

    • 使用 Expect 而没有 Stub 方法,行吗? ayende.com/Wiki/…
    • 是否可以对嵌套对象执行相同的操作,例如 var data = _Executor.ExecuteSql(query, new { CustomerID = new CustomerId{ id=100}, AnotherId1= id2, AnotherId2 = id3 } );
    猜你喜欢
    • 1970-01-01
    • 2019-12-30
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多