Nie mogę sprawdzić, czy mój test jednostki działa prawidłowo. Działa w teście integracji, który mam, gdzie faktycznie trafi do magazynu tabel Azure. Problem, który prawdopodobnie jest kpiną z usługi QueryableEntities, która powoduje retransmitowanie kwerendy ze strony próbnej, ale zwraca DataServiceQuery z klasy ServiceContext. Czy jest możliwe utworzenie kodu pośredniczącego typu DataServiceQuery, który zwraca Queryable?W jaki sposób mogę przetestować zapytanie o tabelę Windows Azure za pomocą kodu pośredniczącego za pomocą funkcji Moq?
To jest mój kod:
test
[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);
}
interfejs IServiceContect
public interface IServiceContext<TEntity>
{
IQueryable<TEntity> QueryableEntities {get;}
}
ServiceContext Klasa
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);
}
}
}
słownik Repository
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;
}}
Dostaję następujący błąd
Błąd:
ArgumentNullException was unhandeled by user code
Value cannot be null.
Parameter name: query
pojawia się błąd podczas wywoływania .AsTableServiceQuery() w następującym wierszu w DictionaryRepository klasa:
var q = this.Context.QueryableEntities.Where(x => x.PartitionKey == Dictionary.ToString() && x.RowKey == word).AsTableServiceQuery();
Rozważmy zawijania kodu Microsoft we własnej klasie, która implementuje uproszczony interfejs. – TrueWill