1
votes

Now this is just strange:

The code as it is below works fine in a NUnit unit test with RhinoMocks (the assert passes).

This is creating an IndexSearcher in the code. Now if I use the mocked version of Get (swap the commented assignment of IndexSearcher) so now the searcher is returned by the mock, it doesn't pass the assertion.

Can anyone figure out why that is? (NUnit 2.5.2 - RhinoMocks 3.6 - Lucene 2.9.2)

    [Test]
    public void Test()
    {

        ISearcherManager searcherManager = _repository.StrictMock<ISearcherManager>();
        Directory directory = new RAMDirectory();
        IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true);

        searcherManager.Expect(item => item.Get()).Return(new IndexSearcher(writer.GetReader())).Repeat.AtLeastOnce();

        _repository.ReplayAll();

        //searcherManager.Get();

        Document doc = new Document();
        doc.Add(new Field("F", "hello you", Field.Store.YES, Field.Index.ANALYZED));
        writer.AddDocument(doc);

        IndexSearcher searcher = searcherManager.Get();
        //IndexSearcher searcher = new IndexSearcher(writer.GetReader());
        QueryParser parser = new QueryParser("F", new StandardAnalyzer());
        Query q = parser.Parse("hello");
        TopDocs hits = searcher.Search(q, 2);

        Assert.AreEqual(1, hits.totalHits);
    }
1
When the assert fails, what's the reason? Are you getting something from Rhino.Mocks?PatrickSteele
The assert fails because there are no results. I checked and I still get a writer to build the searcher.Khash

1 Answers

1
votes

I'm not familiar with Lucene, but the only difference I see is that via the Expect call, you are creating your IndexSearcher before adding the document to the writer. In the code that is commented out, the creation of the IndexSearcher is happening after you add the document to the writer. Is that an important distinction?