Skip to content

Commit 0f67a66

Browse files
committed
Added sql exception to the test
1 parent 383cd72 commit 0f67a66

File tree

21 files changed

+237034
-5
lines changed

21 files changed

+237034
-5
lines changed

ConsoleApplication3/GetXmlDataTest.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Data;
3+
using System.Data.Common;
4+
using System.Data.SqlClient;
35
using System.Data.SqlTypes;
46
using NUnit.Framework;
57
using Moq;
@@ -65,6 +67,16 @@ public void ReaderReturnsEmptyString()
6567
RepoResult = Repo.LoadPerson(SqlConn.Object, SqlCommand.Object, mapper.MapFromRawXmlString);
6668
}
6769

70+
71+
[Test]
72+
[ExpectedException(typeof(SqlException))]
73+
public void Cannot_ConnectToDataBase_ThrowsSqlException()
74+
{
75+
var mapper = new PersonMapper();
76+
SqlConn.Setup(x => x.Open()).Callback(ThrowSqlException);
77+
RepoResult = Repo.LoadPerson(SqlConn.Object, SqlCommand.Object, mapper.MapFromRawXmlString);
78+
}
79+
6880
[Test]
6981
public void DataReader_Called_Read_And_GetValue0_JustOneTime()
7082
{
@@ -120,7 +132,18 @@ public IDataReader ThrowError()
120132
}
121133

122134

123-
135+
#region throwing sql exceptions which is a sealed class.
136+
public void ThrowSqlException()
137+
{
138+
throw Instantiate<SqlException>();
139+
}
140+
141+
public static T Instantiate<T>() where T : class
142+
{
143+
return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(T)) as T;
144+
}
145+
#endregion
146+
124147

125148
public string GoodPersonRawXml()
126149
{

ConsoleApplication3/Program.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Data.SqlClient;
66
using System.Diagnostics.Contracts;
77
using System.IO;
8+
using System.Reflection;
89
using System.Xml.Serialization;
10+
using log4net;
911

1012

1113
namespace ConsoleApplication3
@@ -30,18 +32,22 @@ static void Main(string[] args)
3032
/// <typeparam name="TOutEntity"></typeparam>
3133
public class CallStoredProcWithXmlOut<TOutEntity>
3234
{
35+
36+
3337
public TOutEntity Execute(IDbConnection connection, IDbCommand sqlCommand, Func<string, TOutEntity> mapper)
3438
{
35-
string rawXml;
39+
string rawXml = null;
40+
3641
using (connection)
3742
{
3843
connection.Open();
3944
sqlCommand.Connection = connection;
4045
var reader = sqlCommand.ExecuteReader();
41-
reader.Read();//just need to advance the reader
42-
rawXml = (string)reader.GetValue(0);
43-
connection.Close();//IDisposable should catch this in the using, but here for good measure.
46+
reader.Read(); //just need to advance the reader
47+
rawXml = (string) reader.GetValue(0);
48+
connection.Close(); //IDisposable should catch this in the using, but here for good measure.
4449
}
50+
4551
var mapperResult = mapper.Invoke(rawXml);
4652
return mapperResult;
4753
}

ConsoleApplication3/SampleSqlUnderUnitTest.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<WarningLevel>4</WarningLevel>
6767
</PropertyGroup>
6868
<ItemGroup>
69+
<Reference Include="log4net">
70+
<HintPath>..\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath>
71+
</Reference>
6972
<Reference Include="Microsoft.QualityTools.Testing.Fakes, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
7073
<Reference Include="Moq">
7174
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>

ConsoleApplication3/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="log4net" version="2.0.0" />
34
<package id="Moq" version="4.0.10827" />
45
<package id="NUnit" version="2.6.0.12054" />
56
</packages>
Binary file not shown.

0 commit comments

Comments
 (0)