← Back to team overview

nunit-core team mailing list archive

Re: [Bug 809679] [NEW] Assert.IsAssignableFrom is not implemented properly

 

This has come up in the past and there's some logic in what you say. OTOH,
the API has worked the same way for a number of years and we don't like to
break people's code.

We could consider changing it in NUnit 3.0, since it's a major version upgrade,
but I'd like to see some support for the change from users first. Can you
start a discussion on the nunit-discuss list about this change? We would want
to look at both the classic Assert you refer to here and the constraint-based
equivalent Assert.That(x, Is.AsignableFrom(y));

Meanwhile, this bug will remain unprioritized.

Charlie

On Tue, Jul 12, 2011 at 8:36 PM, Wallace Kelly
<809679@xxxxxxxxxxxxxxxxxx> wrote:
> Public bug reported:
>
> I'm using 2.5.10.11092.
>
> I think the Assert.IsAssignableFrom is not implemented correctly. It
> does not work like Type.IsAssignableFrom. The unit tests below
> illustrate the problem. I expected both of the following to pass:
>
> class Room { };
> class Kitchen : Room { };
>
> [Test]
> public void AssertIsAssignableFrom_WorksLike_TypeIsAssignableFrom()
> {
>    Assert.IsTrue(typeof(Room).IsAssignableFrom(typeof(Kitchen)));
>    Assert.IsAssignableFrom(typeof(Room), new Kitchen());
> }
>
> I looked at the source in TypeConstraints.cs, AssignableFromConstraint,
> Matches. I think it is backwards. It has this:
>
>  return actual != null &&
> actual.GetType().IsAssignableFrom(expectedType);
>
> It should be this:
>
>  return actual != null && expected != null &&
> expectedType.IsAssignableFrom(actual.GetType());
>
> I looked at the unit tests for this method in TypeAssertTest.cs. None of
> the unit tests exercise the case of class hierarchy.
>
> This blog post may be helpful:
> http://codegoeshere.blogspot.com/2009/02/typeisassignablefrom-seems-backwards.html
>
> ** Affects: nunitv2
>     Importance: Undecided
>         Status: New
>
> --
> You received this bug notification because you are the registrant for
> NUnit V2.
> https://bugs.launchpad.net/bugs/809679
>
> Title:
>  Assert.IsAssignableFrom is not implemented properly
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunitv2/+bug/809679/+subscriptions
>

-- 
You received this bug notification because you are a member of NUnit
Developers, which is subscribed to NUnit V2.
https://bugs.launchpad.net/bugs/809679

Title:
  Assert.IsAssignableFrom is not implemented properly

Status in NUnit V2 Test Framework:
  New

Bug description:
  I'm using 2.5.10.11092.

  I think the Assert.IsAssignableFrom is not implemented correctly. It
  does not work like Type.IsAssignableFrom. The unit tests below
  illustrate the problem. I expected both of the following to pass:

  class Room { };
  class Kitchen : Room { };

  [Test]
  public void AssertIsAssignableFrom_WorksLike_TypeIsAssignableFrom()
  {
      Assert.IsTrue(typeof(Room).IsAssignableFrom(typeof(Kitchen)));
      Assert.IsAssignableFrom(typeof(Room), new Kitchen());
  }

  I looked at the source in TypeConstraints.cs,
  AssignableFromConstraint, Matches. I think it is backwards. It has
  this:

   return actual != null &&
  actual.GetType().IsAssignableFrom(expectedType);

  It should be this:

   return actual != null && expected != null &&
  expectedType.IsAssignableFrom(actual.GetType());

  I looked at the unit tests for this method in TypeAssertTest.cs. None
  of the unit tests exercise the case of class hierarchy.

  This blog post may be helpful:
  http://codegoeshere.blogspot.com/2009/02/typeisassignablefrom-seems-backwards.html

To manage notifications about this bug go to:
https://bugs.launchpad.net/nunitv2/+bug/809679/+subscriptions


References