2012-02-05 12 views
5

मुझे अपना यूनिट परीक्षण ठीक से काम करने के लिए नहीं मिल सकता है। यह एक एकीकरण परीक्षण में काम करता है जहां यह वास्तव में Azure टेबल संग्रहण को हिट करेगा। मुझे लगता है कि समस्या क्वेरी का उपयोग कर रहा है QueryableEntities जो नकली से क्वेरी करने योग्य है लेकिन यह DataServiceQuery सेवा कॉन्टेक्स्ट क्लास से लौटाता है। क्या प्रकार का स्टब बनाना संभव है DataServiceQuery जो एक प्रश्नोत्तरी वापस लौटाता है?मैं एमक का उपयोग कर स्टब के साथ विंडोज एज़ूर टेबल क्वेरी का परीक्षण कैसे करूं?

यह मेरा कोड है:

टेस्ट

[TestMethod] 
    public void GetAExistingWordInStorageShouldReturnCorrectWord() 
    { 

     Word expected = new Word(Dictionaries.Swedish.ToString(), "Word", "Word"); 

     List<Word> Words = new List<Word>(); 
     Words.Add(new Word(Dictionaries.Swedish.ToString(), "Word", "Word")); 

     IQueryable<Word> WordQueryable = Words.AsQueryable<Word>(); 

     var mock = new Mock<IServiceContext<Word>>(); 
     mock.Setup(x => x.QueryableEntities).Returns(WordQueryable); 

     DictionaryRepository dr = new DictionaryRepository(Models.Dictionaries.Swedish, "testdictionaries"); 
     dr.Context = mock.Object; 

     Word result = dr.GetWord(expected.Text, false); 

     Assert.AreEqual(expected, result); 
    } 

IServiceContect इंटरफ़ेस

public interface IServiceContext<TEntity> 
{ 
    IQueryable<TEntity> QueryableEntities {get;} 
} 

ServiceContext कक्षा

public class ServiceContext<TEntity> : TableServiceContext, IServiceContext<TEntity> where TEntity : TableServiceEntity 
{ 

    private readonly string tableName; 

    public ServiceContext(CloudStorageAccount account, String tableName) 
     : base(account.TableEndpoint.ToString(), account.Credentials) 
    { 
     this.tableName = tableName; 
     this.IgnoreResourceNotFoundException = true; 
    } 

    public IQueryable<TEntity> QueryableEntities 
    { 
     get 
     { 
      return CreateQuery<TEntity>(tableName); 
     } 
    } 

} 

शब्दकोश भंडार

 public class DictionaryRepository : IDictionaryRepository 
{ 
    public Dictionaries Dictionary { get; set; } 
    public String TableName; 

    public IServiceContext<Word> Context; 

    public DictionaryRepository(Dictionaries dictionary) 
     : this(dictionary, "dictionaries") 
    { 
    } 

    public DictionaryRepository(Dictionaries dictionary, String tableName) 
    { 
     Dictionary = dictionary; 
     this.TableName = tableName; 
     CloudStorageAccount account = CloudStorageAccount.Parse(***); 
     Context = new ServiceContext<Word>(account, this.TableName); 
    } 

    public List<Tile> GetValidTiles() 
    { 
     throw new NotImplementedException(); 
    } 

    public Type ResolveEntityType(String name) 
    { 
     return typeof(Word); 
    } 

    public Word GetWord(string word, Boolean useCache = false) 
    { 

     var q = this.Context.QueryableEntities.Where(x => x.PartitionKey == Dictionary.ToString() && x.RowKey == word).AsTableServiceQuery(); 

     Word result = q.Execute().SingleOrDefault(); 

     if (result == null) 
      return null; 

     return result; 

    }} 

मैं निम्न त्रुटि

त्रुटि हो रही है:

ArgumentNullException was unhandeled by user code 
    Value cannot be null. 
    Parameter name: query 

जब .AsTableServiceQuery (बुला मैं त्रुटि मिलती है) निम्नलिखित पंक्ति पर DictionaryRepository वर्ग:

var q = this.Context.QueryableEntities.Where(x => x.PartitionKey == Dictionary.ToString() && x.RowKey == word).AsTableServiceQuery(); 
+0

माइक्रोसॉफ्ट कोड लपेटकर पर विचार करें अपनी खुद की कक्षा में जो एक सरलीकृत इंटरफ़ेस लागू करता है। – TrueWill

उत्तर

1

आपको त्रुटि का उल्लेख नहीं किया है आप कर रहे हैं, लेकिन जब से QueryableEntities एक केवल पढ़ने के लिए संपत्ति है mock.Setup के बजाय mock.SetupGet उपयोग करके देखें।

संपादित करें:

आगे समस्या इस पर ध्यान देने कि .AsTableServiceQuery() विस्तार विधि, IQueryable<T> एक DataServiceQuery<T> को कास्ट करने के लिए जो अशक्त अपवाद के कारण विफल रहता है प्रयास करता है।

वहाँ कैसे तालिका भंडारण है कि आप बाहर की मदद करनी चाहिए साथ इकाई परीक्षण करने के बारे में फ्रेडेरिक Boerr द्वारा एक पोस्ट है। Windows Azure Storage: TDD and mocks

+0

काम नहीं किया। मैंने अपने प्रश्न को उस त्रुटि के साथ अपडेट किया है जो मुझे मिल रहा है। – Frej

0

मुझे पता है कि आपने विशेष रूप से मोक का उपयोग करके यह कैसे किया है, और मेरे पास इसका जवाब नहीं है, लेकिन मुझे पता चला कि नकली का उपयोग करके कुछ कैसे करना है।

http://azurator.blogspot.com/2013/07/unit-testing-azure-table-storage-queries.html

अनिवार्य रूप से आपको लगता है कि Expression वस्तु क्वेरी का उपयोग कर रहा है पढ़ता है और लागू होता है CloudTableQuery<T> पर एक शिम बना सकते हैं कि इस तरह से अपनी IEnumerable कोड का उपयोग करने के लिए एक ही तर्क:

[TestMethod] 
public void here_is_my_test() 
{ 
    IEnumerable<MyEntityType> fakeResults = GetFakeResults(); 

    using (ShimsContext.Create()) 
    { 
     InterceptCloudTableQueryExecute<MyEntityType>(fakeResults); 

     DoQuery(); 

     AssertStuff(); 
    } 
} 

public void InterceptCloudTableQueryExecute<T>(IEnumerable<T> result) 
{ 
    var query = result.AsQueryable(); 

    ShimCloudTableQuery<T>.AllInstances.Execute = (instance) => 
    { 
     // Get the expression evaluator. 
     MethodCallExpression ex = (MethodCallExpression)instance.Expression; 

     // Depending on how I called CreateQuery, sometimes the objects 
     // I need are nested one level deep. 
     if (ex.Arguments[0] is MethodCallExpression) 
     { 
      ex = (MethodCallExpression)ex.Arguments[0]; 
     } 

     UnaryExpression ue = ex.Arguments[1] as UnaryExpression; 

     // Get the lambda expression 
     Expression<Func<T, bool>> le = ue.Operand as Expression<Func<T, bool>>; 

     query = query.Where(le); 
     return query; 
    }; 
} 
संबंधित मुद्दे