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 am new to using TypeMock and currently checking how we can use it in our C# app.
I am facing an issue with using ExpectAndThow().
The issue is that the exception that I want to throw is from a Sybase library and it doesnot have a constructor for it.
Code to test is:
try
{
m_Connection.Open();
}
catch (SAException sa)
{
LogMessage(sa);
}

SAConnection is thrown when Open is called on a SAConnection object which is from a Sybase library.

How can I test this code to simulate throwing SAException ?
I cannot create a new SAException object in my test code since it's constructor is not exposed by Sybase library.
I also tried to use a mock object of SAException but that gives compiler error.

Thanks,
-Vinay
asked by vtandasi (1.2k points)

4 Answers

0 votes
You can create a fake of the Sybase exception and then throw it - using a fake with Members.CallOriginal is a nice easy way to get an instance of an object that has no public constructor. You could always use reflection also...but I don't go that route unless I'm doing something where I have to worry about constructor arguments being a specific thing:

SAException ex = Isolate.Fake.Instance<SAException>(Members.CallOriginal);

Isolate.WhenCalled(() => whateverYourObjectUnderTestIs.Open()).WillThrow(ex);

//Act
//Do you're call here that would throw the exception

//Assert
//Test for your exception being handled...


HTH
answered by boo (21.8k points)
0 votes
Thanks for the quick reply.

The syntax :
Isolate.WhenCalled(() => whateverYourObjectUnderTestIs.Open()).WillThrow(ex);

gives a compile error due to "(()=> ". How do I correct this?
Also, Open() is called from within another constructor. Something like this:

public DBAccess(String connectionString)
{
try{
m_Connection = new SAConnection(connectionString.ToString());
m_Connection.Open();
}
catch (SAException sa){
LogMessage(sa);
}
}


How can I use a code like:
Isolate.WhenCalled(() => whateverYourObjectUnderTestIs.Open()).WillThrow(ex);

in such a scenario? The call to Open is in the c'tor of whateverYourObjectUnderTestIs.


Thanks,
-Vinay
answered by vtandasi (1.2k points)
0 votes
Are you using 2.0 still? Or VB? Syntax of 'Isolate.WhenCalled(() => fake.Method()).WillThrow(ex)' should be working for you.

To your questions though, I'm not sure of your exact code outside of this post, so let's see if I can come up with something 'like'.

So the first thing you would need to do is have a fake for the object with the Open method:

ClassWithOpenMethod fake = Isolate.Fake.Instance<ClassWithOpenMethod>(Members.CallOriginal);


The Members.CallOriginal will allow the fake to act just like a real instance of ClassWithOpenMethod - so for all intents and purposes, it's not really a fake - but I digress.

Anyway, now you're going to have Isolator use that fake on the next instance of ClassWithOpenMethod that is created:
Isolate.Swap.NextInstance<ClassWithOpenMethod>().With(fake);


You already have this from before where you throw your exception:
Isolate.Verify.WhenCalled(() => fake.Open()).WillThrow(ex);


So in your code that you posted, you'd have:

[Isolated(), TestMethod()]
public void DBAccessConstructor_InvalidConnectionString_ThrowsSAException
{
   //Arrange
   SAException saExceptionFake = Isolate.Fake.Instance<SAException>(Members.CallOriginal);

   SAConnection saConnectionFake = Isolate.Fake.Instance<SAConnection>();
   Isolate.Swap.NextInstance<SAConnection>().With(fake);

   Isolate.WhenCalled(() => saConnectionFake.Open()).WillThrow(saExceptionFake);

   //Act
   try
   {
       DBAccess target = new DBAccess("whatever");
       
       //Assert
       Assert.Fail("Expected Exception");
   }
   catch(SAException)
   {
       //Assert
       //expected - nothing further to check...
   }
}
answered by boo (21.8k points)
0 votes
It's working now. I was using 2.0. Changing to 3.5 corrected the issue .
Thanks for the response.
answered by vtandasi (1.2k points)
...