我想要一个解决方案,其中包括一个 SQL Server 数据库项目和一个单元测试项目。 (+ 集成测试项目?)
该解决方案将由 azure devops 上的 git 存储库进行源代码控制,理想情况下,我希望有一个管道启动 LocalDB (mssqllocaldb) 数据库,运行数据库项目的脚本/datpac,以使其达到速度,然后运行应该能够访问本地服务器上新创建的数据库的单元测试。
我正在尝试的测试相当简单:(目前使用 dapper,一旦我可以让这个测试工作,它将连同连接字符串一起移动到 api 项目):
[TestMethod]
public void TestDBConnection()
{
var connString = "Server=(localdb)\\mssqllocaldb;Database=custom_db_name_here;Trusted_Connection=True;";
using (var connection = new SqlConnection(connString))
{
var result = connection.Query<int>(sql: "select 1", commandType: CommandType.Text);
Assert.AreEqual(result.Count(), 1);
Assert.AreEqual(result.FirstOrDefault(), 1);
}
}
我没有很多 devops 经验,这是 azure devops 生成的 yaml 文件,我添加了
start mssqllocaldb
任务。
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller@1
- task: NuGetCommand@2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild@1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: PowerShell@2
displayName: 'start mssqllocaldb'
inputs:
targetType: 'inline'
script: 'sqllocaldb start mssqllocaldb'
# Publish probably goes here, not sure how though?
- task: VSTest@2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
我不确定如何创建新数据库、添加所有结构(表、过程等)和数据。