如何在HttpSessionStateBase中正确模拟KeysCollection?

时间:2011-08-09 00:04:41

标签: c# unit-testing asp.net-mvc-3 session

我从这里的示例中设置了这个模拟会话对象:How to MOQ an Indexed property

/// <summary>
/// HTTP session mockup.
/// </summary>
internal sealed class HttpSessionMock : HttpSessionStateBase
{
    private readonly Dictionary<string, object> objects = new Dictionary<string, object>();

    public override object this[string name]
    {
        get { return (objects.ContainsKey(name)) ? objects[name] : null; }
        set { objects[name] = value; }
    }
}

产生错误的一些示例代码......

var mockSession = new HttpSessionMock();
var keys = mockSession.Keys;

错误:未实施方法或操作。

我需要实现Keys属性,但不能创建KeysCollection对象。

这样做的最佳方式是什么?

编辑:[解决方案]

我最终根据给出的答案更改了HttpSessionMock。这就是我最终的结果。 (我还添加了对System.Linq的引用)。

internal sealed class HttpSessionMock : HttpSessionStateBase
{
    private readonly NameValueCollection objects = new NameValueCollection();

    public override object this[string name]
    {
        get { return (objects.AllKeys.Contains(name)) ? objects[name] : null; }
        set { objects[name] = (string)value; }
    }

    public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return objects.Keys; }
    }
}

注意:这个模拟会话只存储字符串,而不是对象。

3 个答案:

答案 0 :(得分:16)

我发现原始方法和接受的解决方案的组合允许存储对象和实现密钥属性:

public class HttpSessionMock : HttpSessionStateBase
{
    private readonly NameValueCollection keyCollection = new NameValueCollection();
    private readonly Dictionary<string, object> objects = new Dictionary<string, object>();

    public override object this[string name]
    {
        get
        {
            object result = null;

            if (objects.ContainsKey(name))
            {
                result = objects[name];
            }

            return result;

        }
        set
        {
            objects[name] = value;
            keyCollection[name] = null;
        }
    }

    public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return keyCollection.Keys; }
    }
}

答案 1 :(得分:4)

一种方式:

internal sealed class HttpSessionMock : HttpSessionStateBase
{
    public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return _collection.Keys; }
    }

    private readonly NameValueCollection _collection = new NameValueCollection();
}

答案 2 :(得分:0)

更新: 供您参考,以下是.NET框架中KeysCollection的源代码:

public class KeysCollection : ICollection, IEnumerable
{
    // Fields
    private NameObjectCollectionBase _coll;

    // Methods
    internal KeysCollection(NameObjectCollectionBase coll)
    {
        this._coll = coll;
    }

    public virtual string Get(int index)
    {
        return this._coll.BaseGetKey(index);
    }

    public IEnumerator GetEnumerator()
    {
        return new NameObjectCollectionBase.NameObjectKeysEnumerator(this._coll);
    }

    void ICollection.CopyTo(Array array, int index)
    {
        if (array == null)
        {
            throw new ArgumentNullException("array");
        }
        if (array.Rank != 1)
        {
            throw new ArgumentException(SR.GetString("Arg_MultiRank"));
        }
        if (index < 0)
        {
            throw new ArgumentOutOfRangeException("index", SR.GetString("IndexOutOfRange", new object[] { index.ToString(CultureInfo.CurrentCulture) }));
        }
        if ((array.Length - index) < this._coll.Count)
        {
            throw new ArgumentException(SR.GetString("Arg_InsufficientSpace"));
        }
        IEnumerator enumerator = this.GetEnumerator();
        while (enumerator.MoveNext())
        {
            array.SetValue(enumerator.Current, index++);
        }
    }

    // Properties
    public int Count
    {
        get
        {
            return this._coll.Count;
        }
    }

    public string this[int index]
    {
        get
        {
            return this.Get(index);
        }
    }

    bool ICollection.IsSynchronized
    {
        get
        {
            return false;
        }
    }

    object ICollection.SyncRoot
    {
        get
        {
            return ((ICollection) this._coll).SyncRoot;
        }
    }
}