c# - 单元测试中的元素在完成后仍待处理

标签 c# unit-testing resharper

运行测试后,我在 Resharper 中看到此警告,所有测试均通过。

2018.08.09 11:11:58.524 WARN Element Data.Tests.Infra.IntegrationTests.ResolvedIdentityTests was left pending after its run completion. 2018.08.09 11:11:58.524 WARN Element Data.Tests.Infra.IntegrationTests.ResolvedIdentityTests.Reso was left pending after its run completion.

它们是在测试数据库中设置一些 SQL 的集成测试,然后针对该数据库运行测试。

这是完整的测试类:

namespace Data.Tests.Infra.IntegrationTests
{
    using System;
    using System.Data.SqlClient;
    using System.Threading.Tasks;
    using Dapper;
    using Infrastructure.Models;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public sealed class ResolvedIdentityTests
    {
        [ClassInitialize]
        public static void Initialise(TestContext context)
        {
            const string sql = @"insert into infra.tblUnresolvedIdentities
                                (DeviceId, Fqdn, TimeConflictOccured)
                                values
                                ('85E33FB5-C321-4EF2-994C-C835F136BA0C', 'unr.test.foo', '2018-08-06 12:16:24.183'),
                                ('D3F32F97-2375-47CC-86E7-37C50ABAC85F', 'unr2.test.foo', '2018-08-06 12:16:24.183')

                                insert into infra.tblOrg ([Name]) values ('rito')
                                declare @orgId int = (select OrgId from infra.tblOrg where [Name] = 'rito');

                                insert into infra.tblSite ([SiteName], [OrgId]) values ('rito.site', @OrgId);
                                declare @siteId int = (select SiteId from infra.tblSite where [SiteName] = 'rito.site');

                                insert into infra.tblDevice
                                (DeviceId, [Name], SiteId)
                                values
                                ('CE810507-C614-4C65-9675-569EEFFDBC9F', 'unr.test.foo', @siteId),
                                ('94FF1C23-0B7E-41CB-A0F8-058CED0465B3', 'blacklisted.test.foo', @siteId)

                                insert into infra.tblBlacklistedAgents
                                (DeviceId, Fqdn)
                                values
                                ('94FF1C23-0B7E-41CB-A0F8-058CED0465B3', 'rit.test.com')";
            RunSql(sql);
        }

        [ClassCleanup]
        public static void Cleanup()
        {
            const string sql = @"delete from infra.tblBlacklistedAgents where DeviceId = '94FF1C23-0B7E-41CB-A0F8-058CED0465B3'
                                 delete from infra.tblUnresolvedIdentities where DeviceId in ('85E33FB5-C321-4EF2-994C-C835F136BA0C', 'D3F32F97-2375-47CC-86E7-37C50ABAC85F')
                                 delete from infra.tblDevice where DeviceID in( 'CE810507-C614-4C65-9675-569EEFFDBC9F', '94FF1C23-0B7E-41CB-A0F8-058CED0465B3')
                                 delete from infra.tblsite where SiteName = 'rito.site'
                                 delete from infra.tblorg where Name = 'rito'
                                 delete from infra.tblResolvedIdentities where ActualDeviceId = 'CE810507-C614-4C65-9675-569EEFFDBC9F'";
            RunSql(sql);
        }

        private static void RunSql(string sql)
        {
            using (var sqlConnection = new SqlConnection(Configuration.InitConfiguration()["ConnectionString"]))
                sqlConnection.Execute(sql);
        }

        [TestMethod]
        public async Task ResolvedIdentityTests_ShouldResolveAnIdentityAndAddRowToResolvedIdentityTable()
        {
            var infra = new Infrastructure.Identities(Configuration.InitConfiguration()["ConnectionString"]);
            await infra.ResolveIdentity(erroneousDeviceId: new Guid("85E33FB5-C321-4EF2-994C-C835F136BA0C"), actualDeviceId: new Guid("CE810507-C614-4C65-9675-569EEFFDBC9F"));

            // now call GetResolvedIdentity so we can verify it was resolved.
            var resolvedIdentity = await infra.GetResolvedIdentity(new Guid("85E33FB5-C321-4EF2-994C-C835F136BA0C"));
            Assert.AreEqual(actual: resolvedIdentity.ResolvedIdentity.ActualDeviceId, expected: new Guid("CE810507-C614-4C65-9675-569EEFFDBC9F"));
            Assert.AreEqual(actual: resolvedIdentity.ResolvedIdentity.SiteName, expected: "rito.site");
            Assert.IsFalse(resolvedIdentity.ResolvedIdentity.Id == -1);
            Assert.AreEqual(actual: resolvedIdentity.ResolutionStatus, expected: IdentityResolutionState.Resolved);
        }

        [TestMethod]
        public async Task GetResolvedIdenity_ShouldResolveToBlacklisted()
        {
            var infra = new Infrastructure.Identities(Configuration.InitConfiguration()["ConnectionString"]);
            var resolvedIdentity = await infra.GetResolvedIdentity(new Guid("94FF1C23-0B7E-41CB-A0F8-058CED0465B3"));

            Assert.AreEqual(actual: resolvedIdentity.ResolutionStatus, expected: IdentityResolutionState.Blacklisted);
            Assert.AreEqual(actual: resolvedIdentity.ResolvedIdentity, expected: null);
        }

        [TestMethod]
        public async Task GetResolvedIdenity_ShouldResolveToStillPending()
        {
            var infra = new Infrastructure.Identities(Configuration.InitConfiguration()["ConnectionString"]);
            var resolvedIdentity = await infra.GetResolvedIdentity(new Guid("D3F32F97-2375-47CC-86E7-37C50ABAC85F"));

            Assert.AreEqual(actual: resolvedIdentity.ResolutionStatus, expected: IdentityResolutionState.Pending);
            Assert.AreEqual(actual: resolvedIdentity.ResolvedIdentity, expected: null);
        }
    }
}

我不知道为什么这些测试仍然悬而未决,或者这些警告是否是我应该关心的事情。

这是在Resharper测试运行器中,在MS测试运行器中运行时不显示警告(但也许它不显示警告?)

最佳答案

关于c# - 单元测试中的元素在完成后仍待处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764422/

相关文章:

c# - LINQ:来自 string[] 的字典

swift - RxSwift 测试现在失败。引入了延迟

Python单元测试断言引发错误

xaml - ElementName 绑定(bind)不解析属性

c# - 从缓存获取对象时,代码试探性无法访问

C# - 将 2 个列表与自定义元素进行比较

c# - 如何使用 ComboBox.ObjectCollection.Item() 属性

ruby-on-rails - 从零开始学习 Ruby 单元测试

c# - 哪个 C# IDE 提供最广泛的 AST 访问?

c# - 不能在 NSSet<TKey> 中使用类