单元测试:修正单元测试文件夹名称为UnitTest

This commit is contained in:
Argo-Surface 2019-01-12 19:23:10 +08:00
parent 54ad1ae1fb
commit 9aab788ea7
14 changed files with 84 additions and 9 deletions

View File

@ -54,7 +54,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MySQL", "MySQL", "{084E2E94
DatabaseScripts\MySQL\install.sql = DatabaseScripts\MySQL\install.sql
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniTest", "UniTest\UniTest.csproj", "{1019D84F-2D0E-4976-947E-10AB16A4E53F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniTest", "UnitTest\UniTest.csproj", "{CFE75C48-F9D5-403A-8419-D07939BBD769}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -82,10 +82,10 @@ Global
{8336F096-4B4A-4710-A1FA-0F5E44CD8D26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8336F096-4B4A-4710-A1FA-0F5E44CD8D26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8336F096-4B4A-4710-A1FA-0F5E44CD8D26}.Release|Any CPU.Build.0 = Release|Any CPU
{1019D84F-2D0E-4976-947E-10AB16A4E53F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1019D84F-2D0E-4976-947E-10AB16A4E53F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1019D84F-2D0E-4976-947E-10AB16A4E53F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1019D84F-2D0E-4976-947E-10AB16A4E53F}.Release|Any CPU.Build.0 = Release|Any CPU
{CFE75C48-F9D5-403A-8419-D07939BBD769}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFE75C48-F9D5-403A-8419-D07939BBD769}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFE75C48-F9D5-403A-8419-D07939BBD769}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFE75C48-F9D5-403A-8419-D07939BBD769}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,15 +1,26 @@
using Microsoft.Extensions.Configuration;
#define SQLite
//#define MySQL
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
#if SQLite
using System;
using System.IO;
using UniTest;
#endif
namespace Bootstrap.DataAccess
{
public class BootstrapAdminStartup
{
private readonly string DBFile;
public BootstrapAdminStartup()
{
var sqlConnectionStrings = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa";
var mysqlConnectionStrings = "Server=10.211.55.2;Database=BA;Uid=argozhang;Pwd=argo@163.com;SslMode=none;";
var mysqlConnectionStrings = "Server=.;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;";
var config = new ConfigurationBuilder().AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("ConnectionStrings:ba", sqlConnectionStrings),
new KeyValuePair<string, string>("DB:0:Enabled", "false"),
@ -23,11 +34,24 @@ namespace Bootstrap.DataAccess
new KeyValuePair<string, string>("DB:2:ConnectionStrings:ba", mysqlConnectionStrings),
new KeyValuePair<string, string>("LongbowCache:Enabled", "false")
}).Build();
var sc = new ServiceCollection();
sc.AddSingleton<IConfiguration>(config);
sc.AddConfigurationManager(config);
sc.AddCacheManager(config);
sc.AddDbAdapter();
#if SQLite
config["DB:1:Enabled"] = "true";
// Copy File
var dbPath = TestHelper.RetrievePath($"UnitTest{Path.DirectorySeparatorChar}DB{Path.DirectorySeparatorChar}UnitTest.db");
DBFile = Path.Combine(AppContext.BaseDirectory, "UnitTest.db");
if (!File.Exists(DBFile)) File.Copy(dbPath, DBFile);
#elif MySQL
config["DB:2:Enabled"]= "true";
#endif
}
}
}

View File

@ -17,6 +17,7 @@ namespace Bootstrap.DataAccess
public void Retrieves_Ok()
{
Exceptions excep = new Exceptions();
excep.Log(new Exception("UnitTest"), null);
Assert.NotEmpty(excep.Retrieves());
}

View File

@ -21,7 +21,15 @@ namespace Bootstrap.DataAccess
[Fact]
public void Retrieves_Ok()
{
var log = new Log();
var log = new Log()
{
UserName = "UnitTest",
ClientAgent = "UnitTest-Agent",
ClientIp = "::",
CRUD = "UnitTest",
RequestUrl = "~/Home/Index"
};
log.Save(log);
Assert.NotEmpty(log.Retrieves());
}
}

View File

@ -138,5 +138,13 @@ namespace Bootstrap.DataAccess
var u = new User();
Assert.True(u.SaveByRoleId("1", new string[] { "1", "2" }));
}
[Fact]
public void RetrieveUserByUserName_Ok()
{
var u = new User();
var usr = u.RetrieveUserByUserName("Admin");
Assert.Equal("Administrator", usr.DisplayName);
}
}
}

30
UnitTest/TestHelper.cs Normal file
View File

@ -0,0 +1,30 @@
using System;
using System.IO;
namespace UniTest
{
public static class TestHelper
{
/// <summary>
/// 获得当前工程解决方案目录
/// </summary>
/// <returns></returns>
public static string RetrieveSolutionPath()
{
var dirSeparator = Path.DirectorySeparatorChar;
var paths = AppContext.BaseDirectory.SpanSplit($"{dirSeparator}.vs{dirSeparator}");
return paths.Count > 1 ? paths[0] : Path.Combine(AppContext.BaseDirectory, $"..{dirSeparator}..{dirSeparator}..{dirSeparator}..{dirSeparator}");
}
/// <summary>
///
/// </summary>
/// <param name="folder"></param>
/// <returns></returns>
public static string RetrievePath(string folder)
{
var soluFolder = RetrieveSolutionPath();
return Path.Combine(soluFolder, folder);
}
}
}

View File

@ -6,9 +6,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>