tags:

views:

85

answers:

1

I have the following so far which I am trying to unit test:

private Mock<IDBFactory> _mockDbFactory;
private IArticleManager _articleManager;

[Setup]
public Setup()
{
      _mockDbFactory = new Mock<IDBFactory>();

      _articleManager = new ArticleManager(_mockDbFactory);
}



[Test]
public void load_article_by_title()
{
    string title = "sometitle";

    // _dbFactory.GetArticleDao().GetByTitle(title);  <!-- need to mock this

    _mockDBFactory.Setup(x => x.GetArticleDao().GetByTitle(It.IsAny<string>()));

    _articleManager.LoadArticle(title);

    Assert.IsNotNull(_articleManager.Article);

}

I get the error:

Invalid setup of a non-overridable member:

+2  A: 

You need to provide a mock for the object returned by GetArticleDao. Something like this:

var _mockDao = new Mock<IArticleDao>();
_mockDao.Setup(x => x.GetByTitle("test")).Returns("A test title");
_mockDBFactory.Setup(x => x.GetArticleDao).Returns(_mockDao);

The syntax is from memory so it may be off. If GetByTitle returns an object you will need to provide a mock implementation for it as well.

Jamie Ide
shouldn't the dbfactory be mocked first, then the method on mockDao?
mrblah
@mrblah, yes but you already had that in the original method so I didn't show it.
Jamie Ide