FluentAssertions错误CS0012:您必须向汇编系统添加引用

发布于 2025-02-06 02:52:37 字数 3020 浏览 1 评论 0原文

我添加了nuget fluentassertions 6.7.0.7.0 在使用.NET框架的测试项目中4.6.1。我从骑手2022.1.1进行测试。

我是这个nuget的新手,我读了 intro 并搜索问题(没有发现)。我来自应该家人试图升级。

我无法建立基本断言。这是初始代码:

    using FluentAssertions;
    using Moq;
    using System;
    using Xunit;

    public class MyTestClass
    {
        [Fact]
        public void GetProvider_ByRemoteName_Works()
        {
            // input
            var desiredRemoteName = "Remote2";
            
            // prepare
            var context = Context.New(); // mocks and stubs
            
            // execute
            var result = context.SomeService.GetProvider(desiredRemoteName);
            
            // verify
            result.Should().NotBeNull();                      // error line
            result.Should().BeOfType<MyProviderClient>();     // error line
        }

构建错误是:

错误CS0012:类型“数据词”是在未引用的汇编中定义的。您必须添加汇编'System.Data,版本= 0.0.0.0,culture =中性,publicKeyToken = B77A5C561934E089'。
错误CS0012:类型“数据列”是在未引用的汇编中定义的。您必须添加汇编'System.Data,版本= 0.0.0.0,culture =中性,publicKeyToken = B77A5C561934E089'。
...
错误CS0012:类型的“ datarow”是在未引用的汇编中定义的。您必须向汇编'system.data,版本= 0.0.0.0,文化=中性,publicKeyToken = B77A5C561934E089'。

添加引用。

我不明白为什么我应该引用此“ System.Data”组件。这似乎不合法。如果我确实提到:

mytestClass.cs:[CS0121]该调用在以下方法或属性之间是模棱两可的:'dataRowAssertionextensions.should(tdatarow)'和dataSetAssertionExtensions.should(tdataset)'

'


此外,''又可以删除误差线和使用 线路提供有效的构建和测试运行。

另外,IDE编辑器指出:

呼叫在以下方法或属性之间是模棱两可的:'dataRowAssertionextensions.should(tdatarow)'和'datasetAssertionextensions.should(tdataset)'

又可以使用Xunit的断言有效:

            // verify
            Assert.NotNull(result);
            Assert.IsType<MyProviderClient>(result);

跟随您的评论,请考虑使用此更新代码:

            // execute
            object result = context.SomeService.GetProvider(desiredRemoteName);

            // verify
            result.Should().BeAssignableTo<IMyInterface>()
               .And.BeOfType<SomeImplementation>()
               .Which
               .Configuration
               .Should()                    // error line
               .NotBeNull();

相同的错误:相同的错误 :发生在最新的.should()呼叫上。

mytestClass.cs:[CS0121]该调用在以下方法或属性之间是模棱两可的:'dataRowAssertionextensions.should(tdatarow)'和dataSetAssertionExtensions.should(tdataset)'

'

被认为是“正常”的do .beoftype&ltt of cople> .beoftype&lt ;&gt;()。到处哪个?我觉得我身边或以Lib的工作方式出了问题。

I added the nuget FluentAssertions 6.7.0 in a test project using .NET Framework 4.6.1. I run tests from Rider 2022.1.1.

I'm new to this nuget and I read the intro and searched for issues (none found). I come from the Should family and trying to upgrade.

I cannot build with basic assertions. Here is the initial code:

    using FluentAssertions;
    using Moq;
    using System;
    using Xunit;

    public class MyTestClass
    {
        [Fact]
        public void GetProvider_ByRemoteName_Works()
        {
            // input
            var desiredRemoteName = "Remote2";
            
            // prepare
            var context = Context.New(); // mocks and stubs
            
            // execute
            var result = context.SomeService.GetProvider(desiredRemoteName);
            
            // verify
            result.Should().NotBeNull();                      // error line
            result.Should().BeOfType<MyProviderClient>();     // error line
        }

The build errors are:

error CS0012: The type 'DataTable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
error CS0012: The type 'DataColumn' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
...
error CS0012: The type 'DataRow' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I don't understand why I should reference this "System.Data" assembly. That does not seem legit. If I do reference it:

MyTestClass.cs: [CS0121] The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should(TDataRow)' and 'DataSetAssertionExtensions.Should(TDataSet)'


Also, removing the error lines and using line provide a valid build and test run.

Also, the IDE editor indicates:

The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should(TDataRow)' and 'DataSetAssertionExtensions.Should(TDataSet)'

Also, using Xunit's assertions works:

            // verify
            Assert.NotNull(result);
            Assert.IsType<MyProviderClient>(result);

Following up on your comments, let's consider this updated code:

            // execute
            object result = context.SomeService.GetProvider(desiredRemoteName);

            // verify
            result.Should().BeAssignableTo<IMyInterface>()
               .And.BeOfType<SomeImplementation>()
               .Which
               .Configuration
               .Should()                    // error line
               .NotBeNull();

The same error occurs on the latest .Should() call.

MyTestClass.cs: [CS0121] The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should(TDataRow)' and 'DataSetAssertionExtensions.Should(TDataSet)'

Is it considered "normal" with FluentAssertions to do .BeOfType<>().Which everywhere? I feel something is wrong on my side or in the way the lib works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2025-02-13 02:52:37

我记得,.NET Framework 4.6.1汇编引用System.Data没有自动添加。对于较新的框架版本,它会自动发生。

As I recall, there's a weird issue with .NET Framework 4.6.1 where the assembly reference to System.Data wasn't automatically added. For newer framework versions, it happens automatically.

绿光 2025-02-13 02:52:37

您列出的IDE错误表明编译器不知道要调用哪个。对于任何依赖于超负荷分辨率的库来说,这并不罕见。

由于看起来您只打算在此上下文中测试返回类型,因此您仅“需要” objectAssertions .should()的过载。因此,您可以避免这样的模棱两可的调用:

object result = context.SomeService.GetProvider(desiredRemoteName);
            
result.Should().BeOfType<MyProviderClient>() // Chained FA assertions are now typed as MyProviderClient
    .Which.Should().BeEquivalentTo(new { Foo = "bar" });

观察:

请记住,值null没有运行时类型,因此断言对象具有某些特定的运行时类型,隐含地断言它不是null。 FA的.beoftype&lt; t&gt;()主张如果断言null引用,则会因特定消息而失败。

The IDE error you listed suggests that the compiler doesn't know which of the .Should() overloads to call. This isn't unusual for any library that relies on overload resolution.

Since it looks like you're only intending to test the return type in this context, you only "need" the ObjectAssertions overload of .Should(). As such, one way you could avoid the ambiguous invocation like this:

object result = context.SomeService.GetProvider(desiredRemoteName);
            
result.Should().BeOfType<MyProviderClient>() // Chained FA assertions are now typed as MyProviderClient
    .Which.Should().BeEquivalentTo(new { Foo = "bar" });

Observe:
Demonstration of chained assertion typing

Remember that the value null does not have a runtime type, so asserting that an object has some specific runtime type implicitly asserts that it is not null. FA's .BeOfType<T>() assertion will fail with a specific message if a null reference is asserted on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文