patterncsharpMinor
Mock/unit test for this IRepositoryService method/class
Viewed 0 times
thismethodmockfortestirepositoryserviceclassunit
Problem
I have the below code:
The method
This has been tested, and for the method
The current unit test I created is as below (using the
```
[TestFixture]
public class GetHomepageBannerSpecs
{
[TestFixture]
public class when_method_is_called : SpecsForRepository
{
private HomepageBanner _bannerA;
private HomepageBanner _bannerB;
private HomepageBanner _bannerC;
private List _results;
private HomepageBanner createHomepageBanner()
{
HomepageBanner banner = new HomepageBanner();
banner.Store();
return banner;
}
protected override void Given()
{
public interface IRepositoryService
{
///
/// This will add conditions like Published = true, Deleted = false and PublisehdDate smaller than NOW
///
///
IRavenQueryable AddConditionsToQueryToLoadOnlyAvailableForFrontend(IRavenQueryable query) where T : BaseObject;
}
public class HomepageBannerRepository : BaseRepository, IHomepageBannerRepository
{
public HomepageBannerRepository(IRepositoryService repositoryService)
: base(repositoryService)
{
}
public List GetHomepageBanners()
{
var q = CreateQuery(loadOnlyItemsAvailableToFrontend: true, autoSortByPriority: true);
var list = q.ToList();
return list;
}
}The method
IRepositoryService.AddConditionsToQueryToLoadOnlyAvailableForFrontend() adds general conditions to any query like Published=True and Deleted=False, as all my objects inherit from BaseObject which contains fields like Published, Deleted etc.This has been tested, and for the method
HomepageBannerRepository.GetHomepageBanners(), I would only like to verify that it was called (using mocks), rather than having to check via unit-testing that only published items are returned.The current unit test I created is as below (using the
SpecsFor framework)```
[TestFixture]
public class GetHomepageBannerSpecs
{
[TestFixture]
public class when_method_is_called : SpecsForRepository
{
private HomepageBanner _bannerA;
private HomepageBanner _bannerB;
private HomepageBanner _bannerC;
private List _results;
private HomepageBanner createHomepageBanner()
{
HomepageBanner banner = new HomepageBanner();
banner.Store();
return banner;
}
protected override void Given()
{
Solution
Nice thing in participating/answering in forums like this is that you learn while you answer questions. I haven't heard about SpecsFor framework. Looks a bit tricky, but will definitely have a look later. Ok, back to your question :)
About your first question, setting up the mock - you can definitely do that, there are a number of overloaded
The code overally looks good except that Ayende suggests (and I completely agree with him) not to wrap RavenDB sessions into repositories. I would just create an extension method to add these filters to
About your first question, setting up the mock - you can definitely do that, there are a number of overloaded
Returns methods accepting delegate/lambda, depending on the number of parameters in the method being setup (here I setup the method to always return the same query regardless of the query passed:GetMockFor(IRepositoryService)
.Setup(x => x.AddConditionsToQueryToLoadOnlyAvailableForFrontend(It.IsAny>()))
.Returns(q => q);The code overally looks good except that Ayende suggests (and I completely agree with him) not to wrap RavenDB sessions into repositories. I would just create an extension method to add these filters to
IRavenQueryable where T:BaseObjectCode Snippets
GetMockFor(IRepositoryService)
.Setup(x => x.AddConditionsToQueryToLoadOnlyAvailableForFrontend(It.IsAny<IRavenQueryable<HomepageBanner>>()))
.Returns(q => q);Context
StackExchange Code Review Q#20644, answer score: 4
Revisions (0)
No revisions yet.