chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi.

I'm trying to make a mocked instance throw an exception when a specific method is called.

[TestMethod]
[ExpectedException(typeof(RepositoryExpectedException))]
public void CreateTest3()
{
    using (RecordExpectations recorder = new RecordExpectations())
    {
        RepositoryNHibernate repNHibernate = new RepositoryNHibernate(typeof(Customer).Name);
        recorder.MockStaticConstructors();
        repNHibernate.Create(null);
        recorder.Throw(new RepositoryExpectedException());
    }

    // Test

    Repository target = new Repository(typeof(Customer).Name);
    target.Create(BuildTestCustomer());
}


I thought (from documentation) that the recorder.Throw() statement would make Create throw the RepositoryExpectedException.

The thing is that is not. What am I doing wrong?

Thanks in advance.

Hugo
asked by hgr (1.1k points)

3 Answers

0 votes
Hi Hugo

The problem here that the type and method you are
mocking are different than the those who are called.

try:
[TestMethod] 
[ExpectedException(typeof(RepositoryExpectedException))] 
public void CreateTest3() 
{ 
    // Mock Repository NHibernate 

    using (RecordExpectations recorder = new RecordExpectations()) 
    { 
        Repository mockTarget = new Repository(typeof(Customer).Name); 
        recorder.MockStaticConstructors(); 
        mockTarget.Create(null); 
        recorder.Throw(new RepositoryExpectedException()); 
    } 

    // Test 
    Repository target = new Repository(typeof(Customer).Name); 
    target.Create(BuildTestCustomer()); 
}


Hope it helps
answered by ohad (35.4k points)
0 votes
Well, but that's the whole idea.
Repository is calling RepositoryNHibernate. I want to test that Repository calls RepositoryNHibernate correctly without actually executing the operations in this last class (because that would involve database operations).

Notice that my previous code works well on the mocking of the static constructors and the RepositoryNHibernate.Create call. It just ignores the exception.

Any other idea?
answered by hgr (1.1k points)
0 votes
Hi Hugo
Can you send me the test solution
so we can create the problem?
I'll send my address off line.
answered by ohad (35.4k points)
...