Skip to content

Commit d5ded6f

Browse files
committed
Merge pull request praeclarum#75 from damirarh/TestsWithExceptions
Asserting exceptions in Metro style tests
2 parents 2157a3f + 29faac0 commit d5ded6f

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

tests/AsyncTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,7 @@ public void TestAsyncTableQueryToFirstAsyncFound ()
570570
Assert.AreEqual(customer.Email, loaded.Email);
571571
}
572572

573-
#if !NETFX_CORE
574573
[Test]
575-
[ExpectedException(typeof(AggregateException))]
576574
public void TestAsyncTableQueryToFirstAsyncMissing ()
577575
{
578576
var conn = GetConnection();
@@ -585,9 +583,8 @@ public void TestAsyncTableQueryToFirstAsyncMissing ()
585583
// query...
586584
var query = conn.Table<Customer>().Where(v => v.Id == -1);
587585
var task = query.FirstAsync();
588-
task.Wait();
586+
ExceptionAssert.Throws<AggregateException>(() => task.Wait());
589587
}
590-
#endif
591588

592589
[Test]
593590
public void TestAsyncTableQueryToFirstOrDefaultAsyncFound ()

tests/DropTableTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public TestDb () : base(TestPath.GetTempFileName ())
3737
}
3838
}
3939

40-
#if !NETFX_CORE
41-
[Test, ExpectedException (typeof (SQLiteException))]
40+
[Test]
4241
public void CreateInsertDrop ()
4342
{
4443
var db = new TestDb ();
@@ -56,8 +55,7 @@ public void CreateInsertDrop ()
5655

5756
db.DropTable<Product> ();
5857

59-
db.Table<Product> ().Count (); // Should throw
58+
ExceptionAssert.Throws<SQLiteException>(() => db.Table<Product> ().Count ());
6059
}
61-
#endif
6260
}
6361
}

tests/ExceptionAssert.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
#if NETFX_CORE
8+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
9+
#else
10+
using NUnit.Framework;
11+
#endif
12+
13+
namespace SQLite.Tests
14+
{
15+
public class ExceptionAssert
16+
{
17+
public static T Throws<T>(Action action) where T : Exception
18+
{
19+
try
20+
{
21+
action();
22+
}
23+
catch (T ex)
24+
{
25+
return ex;
26+
}
27+
28+
Assert.Fail("Expected exception of type {0}.", typeof(T));
29+
30+
return null;
31+
}
32+
}
33+
}

tests/SQLite.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<Compile Include="BooleanTest.cs" />
3939
<Compile Include="..\src\SQLite.cs" />
4040
<Compile Include="ByteArrayTest.cs" />
41+
<Compile Include="ExceptionAssert.cs" />
4142
<Compile Include="InsertTest.cs" />
4243
<Compile Include="CreateTableTest.cs" />
4344
<Compile Include="GuidTests.cs" />
@@ -58,4 +59,4 @@
5859
</Compile>
5960
<Compile Include="MappingTest.cs" />
6061
</ItemGroup>
61-
</Project>
62+
</Project>

tests/SQLiteMetroTests/SQLiteMetroTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
<Compile Include="..\DropTableTest.cs">
143143
<Link>DropTableTest.cs</Link>
144144
</Compile>
145+
<Compile Include="..\ExceptionAssert.cs">
146+
<Link>ExceptionAssert.cs</Link>
147+
</Compile>
145148
<Compile Include="..\GuidTests.cs">
146149
<Link>GuidTests.cs</Link>
147150
</Compile>

0 commit comments

Comments
 (0)