Question

im trying to stub the following line of code in C# using rhino mocks although unsuccessfully. Any suggestions please? This is the line that causes the test to fail

var header = this.repository.Headers.FirstOrDefault(h => h.Id == id);

Full details below, many thanks!

Unit test

  private IRepository _repository;

  [TestInitialize]
  public void SetUp()
  {
    _repository = _mockRepository.Stub<IRepository>();      
    _commandService = new CoreCommandService(_repository);
  }

  [TestMethod]
  public void MyTest()
  {
    // Line that doesn't work
    _repository.Stub(x => x.Headers).Return(SomeThing);
  }

implementation

  // The Headers is stubbed although Id is null

  var header = this.repository.Headers.FirstOrDefault(h => h.Id == id);

  public interface IRepository
  {
    IEntityRepository<Header> Headers { get; }
  }

UPDATE #1

  public interface IEntityRepository<TEntity> : IQueryable<TEntity>,  IDisposable where TEntity : class

UPDATE #2

Using the following example

  var wrapper = new HeadersWrapper(...);
  _repository.Stub(x => x.Headers).Return(wrapper);

Returns the following message when compiling

  HeaderWrapper is not assignable to parameter type IEntityRepository<Header>
Was it helpful?

Solution

Difficult.

I think you have two options.

If you can change the return type of Headers from IEntityRepository<T> to IQueryable<T>, you can return a queryable List<T> for Headers:

IList<T> list = new List<Header> { /* Some Header object */ }
IQueryable<T> queryableList = list.AsQueryable();
_repository.Stub(x => x.Headers).Return(queryableList);

If you can't change the return type, you need to create a test fake (a new class) that derives from IEntityRepository<T>, and wraps a queryable List<T>. You will need to implement all the methods that IEntityRepository<T> and its interfaces define, but just call the relevant function on the queryable List<T>. Then you add a relevant Header to the underlying queryable List<T> as above and return a new instance of this object to stub Headers.

IQueryable<Header> Wrapper would look something like this:

public class HeadersWrapper : IQueryable<Header>
{
    private readonly IQueryable<Header> _queryableSource;

    public HeadersWrapper(IEnumerable<Header> source)
    {
        _queryableSource = source.AsQueryable();
    }

    public IEnumerator<Header> GetEnumerator()
    {
        return _queryableSource.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public Expression Expression
    {
        get { return _queryableSource.Expression; }
    }

    public Type ElementType
    {
        get { return _queryableSource.ElementType; }
    }

    public IQueryProvider Provider
    {
        get { return _queryableSource.Provider; }
    }
}

And you'd use it something like this:

var headers = new List<Header>
{
    new Header { Id = "foo" }
};

var wrapper = new HeadersWrapper(headers);

repo.Stub(x => x.Headers).Return(wrapper);

And of course, you can exchange IQueryable<Header> for IEntityRepository<Header> if you want to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top