This is a migrated thread and some comments may be shown as answers.

Mocking InnerException of SqlException does not work

1 Answer 331 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Normen
Top achievements
Rank 1
Normen asked on 11 Sep 2012, 12:27 PM
Hi there,
I am trying to mock the InnerException-Property of a Mocked SqlException.

[Test]
public void SqlException_Example()
{
    var sqlEx = Mock.Create<SqlException>();
    sqlEx.Arrange(ex => ex.Class).Returns(20);
    sqlEx.Arrange(ex => ex.InnerException).Returns(new Exception("My Test"));
 
    Assert.IsTrue(Mock.IsProfilerEnabled);
    Assert.That(sqlEx.Class, Is.EqualTo(20));
    Assert.That(sqlEx.InnerException, Is.Not.Null); //<-- Fails
}


I will be grateful for any help.

Normen

1 Answer, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 13 Sep 2012, 04:01 PM

Hi Normen,
Thanks again for contacting us.

The property "InnerException" belongs to mscorlib. Therefore, you first need to initialize it. I did it in the following way and working as expected.

static UnitTest1()
{
    Mock.Replace<SqlException, Exception>(x => x.InnerException).In<UnitTest1>(x => x.TestMethod1());
}
  
[TestMethod]
public void TestMethod1()
{
    var sqlEx = Mock.Create<SqlException>();
    sqlEx.Arrange(ex => ex.Class).Returns(20);
    sqlEx.Arrange(ex => ex.InnerException).Returns(new Exception("My Test"));
  
    Assert.IsTrue(Mock.IsProfilerEnabled);
    Assert.AreEqual(sqlEx.Class, 20);
    Assert.IsNotNull(sqlEx.InnerException); //<-- works
}

For detailed information on mscorlib mocking please check out the following link:
http://www.telerik.com/help/justmock/advanced-usage-mscorlib-mocking.html 


Kind Regards,
Ricky
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Tags
General Discussions
Asked by
Normen
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or