简单的 `Assert.IsAssignableFrom` 失败

发布于 2024-10-30 07:00:12 字数 392 浏览 0 评论 0 原文

为什么这个简单的断言语句会失败?从我读到的内容来看,我应该是。不幸的是,由于该功能非常基础,因此没有太多信息。

public interface IDummy{}
public class Dummy : IDummy {}

Assert.IsAssignableFrom<IDummy>(new Dummy());

运行此测试会产生

Expected: assignable from <Application.Tests.ViewModels.IDummy>
  But was:  <Application.Tests.ViewModels.Dummy>

我尝试交换接口和对象端但无济于事。

Why does this simple assert statement fail? From what I've read I should be . Unfortunately, since the functionality is so basic there isn't much information out there.

public interface IDummy{}
public class Dummy : IDummy {}

Assert.IsAssignableFrom<IDummy>(new Dummy());

Running this test yields

Expected: assignable from <Application.Tests.ViewModels.IDummy>
  But was:  <Application.Tests.ViewModels.Dummy>

I have tried swapping the interface and objects side to no avail.

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

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

发布评论

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

评论(2

拿命拼未来 2024-11-06 07:00:12

IsAssignableFrom 的工作原理与您的期望相反。它询问:(值)是否可以从 IDummy 分配。或者:“可以分配给(值)吗?”

来自 XML 文档:
/// 断言可以为对象分配给定类型的值。

您可能需要 Assert.IsInstanceOfType()

IsAssignableFrom works in reverse from what you are expecting. It's asking: Is (the value) Assignable From IDummy. Or: "Is assignable to (value)?"

From the XML doc:
/// Asserts that an object may be assigned a value of a given Type.

You probably want Assert.IsInstanceOfType()

舞袖。长 2024-11-06 07:00:12

为了那些通过 Google(或 Bing :-) 登陆这里的人的利益)

Assert.IsAssignableFrom(objectactual) 旨在检查正在检查的对象是否可以用类型 T 替换实际上,这意味着断言正在测试所检查的对象与类型 T 之间的“is-a”关系

让我们看一些代码(有意简化):

// The Base class
public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfJoining { get; set; }
}

// The derived class
public class Manager : Employee
{
    public IList<Employee> EmployeesReporting { get; set; }
}

现在断言:

// NOTE: using Assert = NUnit.Framework.Assert;
[TestMethod]
public void Test_IsAssignableFrom()
{
    var employee = new Employee();

    // Manager is-a Employee, so the below is true
    Assert.IsAssignableFrom<Manager>(employee);    
}

。 (objectactual) 旨在检查正在测试的对象是否是(顾名思义)类型 T 的实例 - 简而言之,如果“实际”是类型 T 还是从 T 派生的类型

[TestMethod]
public void Test_IsAssignableFrom()
{
    var manager = new Manager();

    // Manager derives from Employee so the below is true
    Assert.IsInstanceOf<Employee>(manager);   
}

编辑
事实证明,这些断言的工作方式与 系统.类型

For the benefit of those who land here through Google (or Bing :-) )

Assert.IsAssignableFrom<T>(object actual) is meant to check if the object being examined can be substituted by the type T. Effectively, what this means is that the assertion is testing an "is-a" relationship between the object examined and the type T.

Let's see some code (intentionally simplified):

// The Base class
public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfJoining { get; set; }
}

// The derived class
public class Manager : Employee
{
    public IList<Employee> EmployeesReporting { get; set; }
}

Now the assertion:

// NOTE: using Assert = NUnit.Framework.Assert;
[TestMethod]
public void Test_IsAssignableFrom()
{
    var employee = new Employee();

    // Manager is-a Employee, so the below is true
    Assert.IsAssignableFrom<Manager>(employee);    
}

Whereas the IsInstanceOf<T>(object actual) is meant to check whether the object being tested is (like the name suggests) an instance of type T - simply put, if "actual" is the type T or a type derived from T

[TestMethod]
public void Test_IsAssignableFrom()
{
    var manager = new Manager();

    // Manager derives from Employee so the below is true
    Assert.IsInstanceOf<Employee>(manager);   
}

EDIT
Turns out, these asserts work the same way as Type.IsAssignableFrom and Type.IsInstanceOf methods on System.Type

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