How do you mock the session object collection using Moq

前端 未结 7 541
忘了有多久
忘了有多久 2020-11-28 22:08

I am using shanselmann\'s MvcMockHelper class to mock up some HttpContext stuff using Moq but the issue I am having is being able to assign something to my mocked session ob

相关标签:
7条回答
  • 2020-11-28 22:43

    I've made a slightly more elaborate Mock than the answer posted by @RonnBlack

    public class HttpSessionStateDictionary : HttpSessionStateBase
    {
        private readonly NameValueCollection keyCollection = new NameValueCollection();
    
        private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
    
        public override object this[string name]
        {
            get { return _values.ContainsKey(name) ? _values[name] : null; }
            set { _values[name] = value; keyCollection[name] = null;}
        }
    
        public override int CodePage
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
    
        public override HttpSessionStateBase Contents
        {
            get { throw new NotImplementedException(); }
        }
    
        public override HttpCookieMode CookieMode
        {
            get { throw new NotImplementedException(); }
        }
    
        public override int Count
        {
            get { return _values.Count; }
        }
    
         public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return keyCollection.Keys; }
    }
    
        public Dictionary<string, object> UnderlyingStore
        {
            get { return _values; }
        }
    
        public override void Abandon()
        {
            _values.Clear();
        }
    
        public override void Add(string name, object value)
        {
            _values.Add(name, value);
        }
    
        public override void Clear()
        {
            _values.Clear();
        }
    
        public override void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }
    
        public override bool Equals(object obj)
        {
            return _values.Equals(obj);
        }
    
        public override IEnumerator GetEnumerator()
        {
            return _values.GetEnumerator();
        }
    
        public override int GetHashCode()
        {
            return (_values != null ? _values.GetHashCode() : 0);
        }
    
        public override void Remove(string name)
        {
            _values.Remove(name);
        }
    
        public override void RemoveAll()
        {
            _values.Clear();
        }
    
        public override void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }
    
        public override string ToString()
        {
            return _values.ToString();
        }
    
        public bool Equals(HttpSessionStateDictionary other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other._values, _values);
        }
    }
    
    0 讨论(0)
提交回复
热议问题