使用RHinomock进行测试

时间:2014-04-25 15:56:16

标签: c# .net testing mocking

我有一个类来测试使用Rhinomock进行测试的方法与普通类不同,因为它的构造函数注入了一个依赖项,该依赖项不是单个接口而是一个Interface对象数组。请帮我设置所有东西,用rhinomock写一个测试。

namespace ClinicalAdvantage.Domain.UserAppSettings
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Newtonsoft.Json.Linq;

    public class Agg : IAgg
    {
        private readonly ISource[] sources;

        public Agg(ISource[] sources)
        {
            this.sources = sources;
         }

        public JObject GetAll()
        {
            var obj = new JObject();
            foreach (var source in this.sources)
            {
                var token = source.GetCurr();
                if (token != null)
                {
                    obj.Add(new JProperty(source.Name, token));
                }
            }

            return obj;
        }
}

ISource是一个有2个实现的接口。 GetALL()迭代每个实现的类对象,并在每个对象中调用GetCurr方法并聚合结果。我必须使用存根GetCurr方法返回标准的Jtoken。我无法创建此类Agg的模拟或ISource的存根。

public interface ISource
    {
        string Name { get; }

        bool Enabled { get; }

        JToken GetCurr();


    }

}

1 个答案:

答案 0 :(得分:0)

这样的事可能有用:

[TestClass]
public class AggTest
{
    private ISource Isource;
    private Agg agg;

    [TestInitialize]
    public void SetUp()
    {
        Isource = MockRepository.GenerateMock<ISource>();
        agg = new Agg(new [Isource]);
    }

    [TestMethod]
    public void GetAll()
    {
        Isource.Stub(x => x.GetCurr()).
            Return(new JToken());

        var jObject = agg.GetAll();

        Assert.IsNotNull(jObject);
        // Do your assertion that all JProperty objects are in the jObject
        // I don't know the syntax
    }
}