Commit ed5fa1cd authored by Jeremy Meng's avatar Jeremy Meng

Simplify Assert.Throws usage.

parent 2ea6ff97
...@@ -74,31 +74,28 @@ public void PingMany(bool preserveOrder) ...@@ -74,31 +74,28 @@ public void PingMany(bool preserveOrder)
} }
[Test] [Test]
// [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"A null key is not valid in this context")]
public void GetWithNullKey() public void GetWithNullKey()
{ {
using (var muxer = Create()) using (var muxer = Create())
{ {
var db = muxer.GetDatabase(); var db = muxer.GetDatabase();
string key = null; string key = null;
//db.StringGet(key); Assert.Throws<ArgumentException>(
ArgumentException ex = Assert.Throws(typeof(ArgumentException), delegate { db.StringGet(key); }) as ArgumentException; () => db.StringGet(key),
Assert.That(ex.Message.Equals( @"A null key is not valid in this context")); "A null key is not valid in this context");
} }
} }
[Test] [Test]
// [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"A null key is not valid in this context")]
public void SetWithNullKey() public void SetWithNullKey()
{ {
using (var muxer = Create()) using (var muxer = Create())
{ {
var db = muxer.GetDatabase(); var db = muxer.GetDatabase();
string key = null, value = "abc"; string key = null, value = "abc";
// db.StringSet(key, value); Assert.Throws<ArgumentException>(
ArgumentException ex = Assert.Throws(typeof(ArgumentException), delegate { db.StringSet(key, value); }) as ArgumentException; () => db.StringSet(key, value),
Assert.That(ex.Message.Equals(@"A null key is not valid in this context")); "A null key is not valid in this context");
} }
} }
...@@ -308,7 +305,6 @@ public void GetWithExpiry(bool exists, bool hasExpiry) ...@@ -308,7 +305,6 @@ public void GetWithExpiry(bool exists, bool hasExpiry)
} }
} }
[Test] [Test]
// [ExpectedException(typeof(RedisServerException), ExpectedMessage = @"A null key is not valid in this context")]
public void GetWithExpiryWrongTypeAsync() public void GetWithExpiryWrongTypeAsync()
{ {
using (var conn = Create()) using (var conn = Create())
...@@ -317,25 +313,26 @@ public void GetWithExpiryWrongTypeAsync() ...@@ -317,25 +313,26 @@ public void GetWithExpiryWrongTypeAsync()
RedisKey key = Me(); RedisKey key = Me();
db.KeyDelete(key); db.KeyDelete(key);
db.SetAdd(key, "abc"); db.SetAdd(key, "abc");
Assert.Throws<RedisServerException>(() =>
{
try try
{ {
var async = db.Wait(db.StringGetWithExpiryAsync(key)); var async = db.Wait(db.StringGetWithExpiryAsync(key));
} }
catch(AggregateException ex) catch (AggregateException ex)
{ {
//throw ex.InnerExceptions[0]; throw ex.InnerExceptions[0];
Assert.That(ex.GetType().Equals(typeof(RedisServerException)));
Assert.That(ex.InnerExceptions[0].Equals(@"A null key is not valid in this context"));
} }
Assert.Fail(); Assert.Fail();
},
"A null key is not valid in this context");
} }
} }
[Test] [Test]
// [ExpectedException(typeof(RedisServerException), ExpectedMessage = "WRONGTYPE Operation against a key holding the wrong kind of value")]
public void GetWithExpiryWrongTypeSync() public void GetWithExpiryWrongTypeSync()
{ {
Exception ex = Assert.Throws(typeof(RedisServerException), delegate Assert.Throws<RedisServerException>(() =>
{ {
using (var conn = Create()) using (var conn = Create())
{ {
...@@ -346,8 +343,8 @@ public void GetWithExpiryWrongTypeSync() ...@@ -346,8 +343,8 @@ public void GetWithExpiryWrongTypeSync()
db.StringGetWithExpiry(key); db.StringGetWithExpiry(key);
Assert.Fail(); Assert.Fail();
} }
}); },
Assert.That(ex.Message.Equals("WRONGTYPE Operation against a key holding the wrong kind of value")); "WRONGTYPE Operation against a key holding the wrong kind of value");
} }
#if FEATURE_BOOKSLEEVE #if FEATURE_BOOKSLEEVE
......
...@@ -202,10 +202,10 @@ public void IntentionalWrongServer() ...@@ -202,10 +202,10 @@ public void IntentionalWrongServer()
} }
[Test] [Test]
//[ExpectedException(typeof(RedisCommandException), ExpectedMessage = "Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot")]
public void TransactionWithMultiServerKeys() public void TransactionWithMultiServerKeys()
{ {
Exception ex = Assert.Throws(typeof(RedisCommandException), delegate { Assert.Throws<RedisCommandException>(() =>
{
using (var muxer = Create()) using (var muxer = Create())
{ {
// connect // connect
...@@ -254,15 +254,14 @@ public void TransactionWithMultiServerKeys() ...@@ -254,15 +254,14 @@ public void TransactionWithMultiServerKeys()
//Assert.IsFalse(cluster.Wait(existsX), "x exists"); //Assert.IsFalse(cluster.Wait(existsX), "x exists");
//Assert.IsFalse(cluster.Wait(existsY), "y exists"); //Assert.IsFalse(cluster.Wait(existsY), "y exists");
} }
}); },
Assert.That(ex.Message.Equals("Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot")); "Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot");
} }
[Test] [Test]
//[ExpectedException(typeof(RedisCommandException), ExpectedMessage = "Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot")]
public void TransactionWithSameServerKeys() public void TransactionWithSameServerKeys()
{ {
Exception ex = Assert.Throws(typeof(RedisCommandException), delegate Assert.Throws<RedisCommandException>(() =>
{ {
using (var muxer = Create()) using (var muxer = Create())
{ {
...@@ -311,8 +310,8 @@ public void TransactionWithSameServerKeys() ...@@ -311,8 +310,8 @@ public void TransactionWithSameServerKeys()
//Assert.IsTrue(cluster.Wait(existsX), "x exists"); //Assert.IsTrue(cluster.Wait(existsX), "x exists");
//Assert.IsTrue(cluster.Wait(existsY), "y exists"); //Assert.IsTrue(cluster.Wait(existsY), "y exists");
} }
}); },
Assert.That(ex.Message.Equals("Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot")); "Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot");
} }
[Test] [Test]
......
...@@ -125,13 +125,16 @@ public void ClientName() ...@@ -125,13 +125,16 @@ public void ClientName()
} }
[Test] [Test]
// [ExpectedException(typeof(RedisCommandException), ExpectedMessage = "This operation has been disabled in the command-map and cannot be used: CONFIG")]
public void ReadConfigWithConfigDisabled() public void ReadConfigWithConfigDisabled()
{ {
using (var muxer = Create(allowAdmin: true, disabledCommands: new[] { "config", "info" })) using (var muxer = Create(allowAdmin: true, disabledCommands: new[] { "config", "info" }))
{ {
var conn = GetServer(muxer); var conn = GetServer(muxer);
Assert.Throws<RedisCommandException>(() =>
{
var all = conn.ConfigGet(); var all = conn.ConfigGet();
},
"This operation has been disabled in the command-map and cannot be used: CONFIG");
} }
} }
[Test] [Test]
...@@ -161,7 +164,7 @@ public void ReadConfig() ...@@ -161,7 +164,7 @@ public void ReadConfig()
} }
[Test] [Test]
public async void TestConfigureAsync() public async System.Threading.Tasks.Task TestConfigureAsync()
{ {
using(var muxer = Create()) using(var muxer = Create())
{ {
......
...@@ -290,10 +290,9 @@ public void KeyPersist() ...@@ -290,10 +290,9 @@ public void KeyPersist()
} }
[Test] [Test]
//[ExpectedException(typeof(NotSupportedException))]
public void KeyRandom() public void KeyRandom()
{ {
Assert.Throws(typeof(NotSupportedException), delegate { wrapper.KeyRandom(); }); Assert.Throws<NotSupportedException>(() => wrapper.KeyRandom());
} }
[Test] [Test]
......
...@@ -24,22 +24,20 @@ public void UnkonwnKeywordHandling_Ignore() ...@@ -24,22 +24,20 @@ public void UnkonwnKeywordHandling_Ignore()
var options = ConfigurationOptions.Parse("ssl2=true", true); var options = ConfigurationOptions.Parse("ssl2=true", true);
} }
[Test] [Test]
//, ExpectedException(typeof(ArgumentException), ExpectedMessage = "Keyword 'ssl2' is not supported")]
public void UnkonwnKeywordHandling_ExplicitFail() public void UnkonwnKeywordHandling_ExplicitFail()
{ {
Exception ex = Assert.Throws(typeof(ArgumentException), delegate { Assert.Throws<ArgumentException>(() => {
var options = ConfigurationOptions.Parse("ssl2=true", false); var options = ConfigurationOptions.Parse("ssl2=true", false);
}); },
Assert.That(ex.Message.Equals("Keyword 'ssl2' is not supported")); "Keyword 'ssl2' is not supported");
} }
[Test] [Test]
//, ExpectedException(typeof(ArgumentException), ExpectedMessage = "Keyword 'ssl2' is not supported")]
public void UnkonwnKeywordHandling_ImplicitFail() public void UnkonwnKeywordHandling_ImplicitFail()
{ {
Exception ex = Assert.Throws(typeof(ArgumentException), delegate { Assert.Throws<ArgumentException>(() => {
var options = ConfigurationOptions.Parse("ssl2=true"); var options = ConfigurationOptions.Parse("ssl2=true");
}); },
Assert.That(ex.Message.Equals("Keyword 'ssl2' is not supported")); "Keyword 'ssl2' is not supported");
} }
} }
} }
...@@ -15,10 +15,9 @@ protected override string GetConfiguration() ...@@ -15,10 +15,9 @@ protected override string GetConfiguration()
} }
[Test] [Test]
//, ExpectedException(typeof(RedisCommandException), ExpectedMessage = "Command cannot be issued to a slave: FLUSHDB")]
public void CannotFlushSlave() public void CannotFlushSlave()
{ {
Exception ex = Assert.Throws(typeof(RedisCommandException), delegate { Assert.Throws<RedisCommandException>(() => {
ConfigurationOptions config = GetMasterSlaveConfig(); ConfigurationOptions config = GetMasterSlaveConfig();
using (var conn = ConnectionMultiplexer.Connect(config)) using (var conn = ConnectionMultiplexer.Connect(config))
{ {
...@@ -26,9 +25,8 @@ public void CannotFlushSlave() ...@@ -26,9 +25,8 @@ public void CannotFlushSlave()
var slave = servers.First(x => x.IsSlave); var slave = servers.First(x => x.IsSlave);
slave.FlushDatabase(); slave.FlushDatabase();
} }
}); },
Assert.That(ex.Message.Equals("Command cannot be issued to a slave: FLUSHDB")); "Command cannot be issued to a slave: FLUSHDB");
} }
[Test] [Test]
......
...@@ -68,17 +68,16 @@ public void Connect() ...@@ -68,17 +68,16 @@ public void Connect()
[Test] [Test]
[TestCase("wrong")] [TestCase("wrong")]
[TestCase("")] [TestCase("")]
//[ExpectedException(typeof(RedisConnectionException), ExpectedMessage = "No connection is available to service this operation: PING")]
public void ConnectWithWrongPassword(string password) public void ConnectWithWrongPassword(string password)
{ {
Exception ex = Assert.Throws(typeof(RedisConnectionException), delegate { Assert.Throws<RedisConnectionException>(() => {
SetExpectedAmbientFailureCount(-1); SetExpectedAmbientFailureCount(-1);
using (var server = Create(password: password, checkConnect: false)) using (var server = Create(password: password, checkConnect: false))
{ {
server.GetDatabase().Ping(); server.GetDatabase().Ping();
} }
}); },
Assert.That(ex.Message.Equals("No connection is available to service this operation: PING")); "No connection is available to service this operation: PING");
} }
} }
} }
...@@ -29,10 +29,9 @@ public void BlankPrefixYieldsSame_String() ...@@ -29,10 +29,9 @@ public void BlankPrefixYieldsSame_String()
} }
} }
[Test] [Test]
//, ExpectedException(typeof(ArgumentNullException))]
public void NullPrefixIsError_Bytes() public void NullPrefixIsError_Bytes()
{ {
Assert.Throws(typeof(ArgumentNullException), delegate { Assert.Throws<ArgumentNullException>(() => {
using (var conn = Create()) using (var conn = Create())
{ {
var raw = conn.GetDatabase(1); var raw = conn.GetDatabase(1);
...@@ -41,10 +40,9 @@ public void NullPrefixIsError_Bytes() ...@@ -41,10 +40,9 @@ public void NullPrefixIsError_Bytes()
}); });
} }
[Test] [Test]
//, ExpectedException(typeof(ArgumentNullException))]
public void NullPrefixIsError_String() public void NullPrefixIsError_String()
{ {
Assert.Throws(typeof(ArgumentNullException), delegate { Assert.Throws<ArgumentNullException>(() => {
using (var conn = Create()) using (var conn = Create())
{ {
var raw = conn.GetDatabase(1); var raw = conn.GetDatabase(1);
...@@ -54,13 +52,12 @@ public void NullPrefixIsError_String() ...@@ -54,13 +52,12 @@ public void NullPrefixIsError_String()
} }
[Test] [Test]
//, ExpectedException(typeof(ArgumentNullException))]
[TestCase("abc")] [TestCase("abc")]
[TestCase("")] [TestCase("")]
[TestCase(null)] [TestCase(null)]
public void NullDatabaseIsError(string prefix) public void NullDatabaseIsError(string prefix)
{ {
Assert.Throws(typeof(ArgumentNullException), delegate { Assert.Throws<ArgumentNullException>(() => {
IDatabase raw = null; IDatabase raw = null;
var prefixed = raw.WithKeyPrefix(prefix); var prefixed = raw.WithKeyPrefix(prefix);
}); });
......
...@@ -259,10 +259,9 @@ public void KeyPersistAsync() ...@@ -259,10 +259,9 @@ public void KeyPersistAsync()
} }
[Test] [Test]
//[ExpectedException(typeof(NotSupportedException))]
public void KeyRandomAsync() public void KeyRandomAsync()
{ {
Assert.Throws(typeof(NotSupportedException), delegate { Assert.Throws<NotSupportedException>(() => {
wrapper.KeyRandomAsync(); wrapper.KeyRandomAsync();
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment