Как выполнить модульное тестирование запроса таблицы Windows Azure с помощью заглушки с помощью Moq?

Я не могу заставить мой модульный тест работать должным образом. Он работает в имеющемся у меня интеграционном тесте, где он действительно попадет в хранилище таблиц Azure. Проблема, я полагаю, заключается в насмешке над свойством QueryableEntities , которое возвращает объект Queryable из макета, но возвращает DataServiceQuery из класса ServiceContext. Можно ли создать заглушку типа DataServiceQuery , возвращающую Queryable?

Это мой код:

Тест

[TestMethod]
    public void GetAExistingWordInStorageShouldReturnCorrectWord()
    {

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

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

        IQueryable WordQueryable = Words.AsQueryable();

        var mock = new Mock>();
        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
{
    IQueryable QueryableEntities {get;}
}

Класс ServiceContext

public class ServiceContext : TableServiceContext, IServiceContext 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 QueryableEntities
    {
        get
        {
            return CreateQuery(tableName);
        }
    }

}

Репозиторий словарей

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

    public IServiceContext 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(account, this.TableName);
    }

    public List 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();

7
задан Frej 9 February 2012 в 13:03
поделиться