Try our conversational search powered by Generative AI!

Mocking IContentResult results

Vote:
 

Hi 

I'm trying to mock IContentResult with Rhino mocks but i'm a bit lost. Basically I want a set of content results with maybe 15 objects to test some paging. So far I've got

var mockResults = MockRepository.GenerateMock<IContentResult<ProductPage>>();

List<ProductPage> productList = new List<ProductPage>();
for (int i = 0; i < 15; i++)
{
productList.Add(new ProductPage{ ProductName = "Product " + i.ToString() });
}

mockResults.Stub(x => x.Items).Return(productList);

This hasn't done me any good. TotalMatching is still zero. Underlying this is SearchResults but trying to stub this out is confusing me.

Does anyone have any advice or tips.

Many Thanks

 

#77615
Nov 21, 2013 16:29
Vote:
 

Hi

 

You should be able to do something like this (I'm using Moq, but it basically the same):

Does this help you?

 

SearchResult<SearchResultItem> searchResult = new SearchResult<SearchResultItem>
{
Hits = new HitCollection<SearchResultItem>
{
Hits = new EditableList<SearchHit<SearchResultItem>>
{
new SearchHit<SearchResultItem>
{
Document = new SearchResultItem
{
LinkUrl = "Url1"
}
},
new SearchHit<SearchResultItem>
{
Document = new SearchResultItem
{
LinkUrl = "Url2"
}
}
},

}
};

SearchResults<SearchResultItem> result = new SearchResults<SearchResultItem>(searchResult);

_resultFacade.Setup(x => x.GetResult(It.IsAny<ISearch<SearchResultItem>>())).Returns(result);

#77656
Nov 22, 2013 11:32
Vote:
 

That is really helpful thanks. A translation into Rhino Mocks still doesn't work sadly. The problem is that I am calling GetContentResults since we are returning pages. This changes the code so that it needs ContentInLanguageReference so it becomes

 //..generating mock results
            var mockResults = MockRepository.GenerateMock<IContentResult<ProductPage>>();

            SearchResult<ContentInLanguageReference> searchResult = new SearchResult<ContentInLanguageReference>
            {
                Hits = new HitCollection<ContentInLanguageReference>
               {
                    Hits = new List<SearchHit<ContentInLanguageReference>>
                    {
                        new SearchHit<ContentInLanguageReference>
                        {
                            Document = new ContentInLanguageReference(new ProductPage { ProductName = "Product 1" })
                        },
                        new SearchHit<ContentInLanguageReference>
                        {
                            Document = new ContentInLanguageReference(new ProductPage { ProductName = "Product 2" })
                        }
                    }
               }
            };

            SearchResults<ContentInLanguageReference> result = new SearchResults<ContentInLanguageReference>(searchResult);
            mockResults.Stub(x => x.SearchResult).Return(result);

But running this through gives a object null error. The failure is instantiating ContentInLanguageReference i,e,

var result = new ContentInLanguageReference(new ProductPage { ProductName = "Product 1" })

Would error.

Anyone any ideas about how to get this instantiated?

 

Many Thanks

   

 

 

 

#77677
Nov 22, 2013 14:27
Vote:
 

We have med the class SearchResultItem our self.

We are searching something like this:

 

ITypeSearch<IBasePageData> searchQuery = _client.Instance()
.Search<IBasePageData>(Language.Norwegian)
.For(searchParameters.Query)
.Take(100);

List<SearchResultItem> searchHits = new List<SearchResultItem>();

ISearch<SearchResultItem> resultQuery = searchQuery.Select(x => new SearchResultItem
{
Heading = x.Heading,
MainIntro = x.GetPreviewTextString,
MainBody = x.MainBody,
LinkUrl = x.PreviewLinkUrl
});

SearchResults<SearchResultItem> results = _resultFacade.GetResult(resultQuery);

 

But I guess you are using the EPi7 version of Find, so that may be a little different from our Cms6 version.

 

 

#77680
Nov 22, 2013 14:55
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.