How can I properly Mock the KeysCollection in Http

2019-04-23 10:58发布

问题:

I setup this mock session object from the example here: 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; }
    }
}

some sample code to produce an error...

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

Error: The method or operation is not implemented.

I need to implement the Keys property, but can't create a KeysCollection object.

What is the best way to do this?

EDIT: [SOLUTION]

I ended up changing the HttpSessionMock based on the answer given. This is what I ended up with. (I also added a reference to 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; }
    }
}

note: this mock session will only store strings, not objects.

回答1:

I found a combination of the original approach and the accepted solution allows both storing of objects and implementing the keys property:

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


回答2:

One way:

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

    private readonly NameValueCollection _collection = new NameValueCollection();
}


回答3:

Updated: For your reference, below is the source code of KeysCollection from .NET framework:

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