Fixed tests

This commit is contained in:
Brandon Smith
2015-08-18 02:50:16 -03:00
parent 4cd0094a2b
commit 07b8ea8450
8 changed files with 83 additions and 38 deletions

View File

@@ -8,7 +8,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Discord.Net.Tests</RootNamespace>
<AssemblyName>Discord.Net.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
@@ -36,6 +36,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
@@ -52,7 +56,7 @@
</Choose>
<ItemGroup>
<Compile Include="Tests.cs" />
<Compile Include="Credentials.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -61,6 +65,9 @@
<Name>Discord.Net</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>

View File

@@ -0,0 +1,29 @@
using Newtonsoft.Json;
using System.IO;
namespace Discord.Net.Tests
{
internal class Settings
{
private const string path = "../../config.json";
public static Settings Load()
{
if (!File.Exists(path))
throw new FileNotFoundException("config.json is missing, rename config.json.example and add data for two separate unused accounts for testing.");
return JsonConvert.DeserializeObject<Settings>(File.ReadAllText(path));
}
public class Account
{
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}
[JsonProperty("user1")]
public Account User1 { get; set; }
[JsonProperty("user2")]
public Account User2 { get; set; }
}
}

View File

@@ -10,21 +10,24 @@ namespace Discord.Net.Tests
public class Tests
{
private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
private DiscordClient _bot1, _bot2;
private Server _testServer;
private Channel _testServerChannel;
private Random _random;
[TestInitialize]
public void Initialize()
private static Settings _settings;
private static DiscordClient _bot1, _bot2;
private static Server _testServer;
private static Channel _testServerChannel;
private static Random _random;
[ClassInitialize]
public static void Initialize(TestContext testContext)
{
_settings = Settings.Load();
_random = new Random();
_bot1 = new DiscordClient();
_bot2 = new DiscordClient();
_bot1.Connect(Settings.Test1_Username, Settings.Test1_Password).Wait();
_bot2.Connect(Settings.Test2_Username, Settings.Test2_Password).Wait();
_bot1.Connect(_settings.User1.Email, _settings.User1.Password).Wait();
_bot2.Connect(_settings.User2.Email, _settings.User2.Password).Wait();
//Cleanup existing servers
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
@@ -58,10 +61,10 @@ namespace Discord.Net.Tests
private void TestCreateRoom(string type)
{
Channel channel = null;
string name = $"test_{_random.Next()}";
string name = $"#test_{_random.Next()}";
AssertEvent<DiscordClient.ChannelEventArgs>(
"ChannelCreated event never received",
() => channel = _bot1.CreateChannel(_testServer, name, type).Result,
() => channel = _bot1.CreateChannel(_testServer, name.Substring(1), type).Result,
x => _bot2.ChannelCreated += x,
x => _bot2.ChannelCreated -= x,
(s, e) => e.Channel.Name == name);
@@ -74,8 +77,8 @@ namespace Discord.Net.Tests
(s, e) => e.Channel.Name == name);
}
[TestCleanup]
public void Cleanup()
[ClassCleanup]
public static void Cleanup()
{
if (_bot1.IsConnected)
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
@@ -86,22 +89,25 @@ namespace Discord.Net.Tests
_bot2.Disconnect().Wait();
}
private void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
private static void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
{
ManualResetEvent trigger = new ManualResetEvent(false);
ManualResetEventSlim trigger = new ManualResetEventSlim(false);
bool result = false;
EventHandler<TArgs> handler = (s, e) =>
{
if (test != null)
{
result |= test(s, e);
trigger.Set();
}
else
result = true;
};
addEvent(handler);
action();
trigger.WaitOne(EventTimeout);
trigger.Wait(EventTimeout);
removeEvent(handler);
Assert.AreEqual(true, result, msg);

View File

@@ -0,0 +1,10 @@
{
"user1": {
"email": "user1@example.com",
"password": "password123"
},
"user2": {
"email": "user2@example.com",
"password": "password456"
}
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net46" />
</packages>