chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Given the following fairly simple class hierarchy (which executes a function through some indirection), I get an IndexOutOfRangeException rather than the exception that should be getting called here.

This worked correctly in 5.3.4 but fails in 6.0.1.

Running VS2008SP1 on WinXPSP3.

public class BaseClass<TSomething> where TSomething : class
{
   protected BaseClass()
   {
      throw new NotImplementedException("Don't call this.");
   }

   protected virtual TResponse Execute<TResponse>(Func<TResponse> action) where TResponse : class
    {
        if (action == null)
        {
            throw new ArgumentNullException("action");
        }
        return action.Invoke();
    }
}

public interface IDoWork
{
  // Placeholder, just for filling in the generic type.
}

public class DerivedClass : BaseClass<IDoWork>
{
   public TResponse ExecutePublic<TResponse>(Func<TResponse> action) where TResponse : class
   {
      return this.Execute(action);
   }
}

[TestFixture]
[Isolated]
public class MyTestFixture
{
   [Test]
   public void MyTest()
   {
      var obj = Isolate.Fake.Instance<DerivedClass>(Members.CallOriginal, ConstructorWillBe.Ignored);
      const Func<string> func = null;
      Assert.Throws<ArgumentNullException>(() => obj.ExecutePublic(func)); // Will be IndexOutOfRange
   }
}
asked by tillig (6.7k points)

6 Answers

0 votes
Hi Tillig,

I'm sending you a patch that should fix the problem.
Please note that in the example you posted you'll still get NotImplementedException from BaseClass since the constructor will be called.
(The ConstructorWillBe flags apply for future instances)

Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
I'll try the patch and let you know via email re: success.

On the ConstructorWillBe flags, it doesn't make sense that they'd be ignored for one usage of the .Instance<T> method but not another. That sort of inconsistent behavior makes things difficult. It would be nice to see it behave the same way all the time.
answered by tillig (6.7k points)
0 votes
I'll try the patch and let you know via email re: success.

On the ConstructorWillBe flags, it doesn't make sense that they'd be ignored for one usage of the .Instance method but not another. That sort of inconsistent behavior makes things difficult. It would be nice to see it behave the same way all the time.
answered by tillig (6.7k points)
0 votes
I'm pretty sure I'm having the same (or seemingly very similar) issue. Only notable differences are that I'm on VS2010 and Windows 7 64-bit.

If you think it would benefit, could I please obtain the patch as well?

Here is my ultra-simplified test case:

using System.Collections.Generic;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;

namespace UnitTests
{
    [TestFixture]
    [Isolated]
    public class TestFixture
    {
        CUT _cut;
        InnerCUT<string> _innerCUT;

        [SetUp]
        public void Start()
        {
            _innerCUT = Isolate.Fake.Instance<InnerCUT<string>>();
            _cut = new CUT();
        }

        [Test]
        public void Add()
        {
            _cut.Add();
            Isolate.Verify.WasCalledWithAnyArguments(() => _innerCUT.Fill(null));
        }
    }

    public class CUT
    {
        public CUT()
        {
        }
        public void Add()
        {
        }
    }

    public class InnerCUT<T> : InnerBaseCUT<T>
    {
    }

    public class InnerBaseCUT<T>
    {
        // This one will throw..
        // System.IndexOutOfRangeException : Index was outside the bounds of the array.
        public void Fill(IEnumerable<T> items)
        {
        }
        // This one will work fine.
        //public void Fill(object items)
        //{
        //}
    }
}
answered by Darin_Creason (3.5k points)
0 votes
Sorry, about the multiple-postings. I received 2 errors from your Apache web-server when trying to post my reply. The 3rd try was charmed (always is right?). ;-)
answered by Darin_Creason (3.5k points)
0 votes
No harm done, I removed the duplicate posts. I will look into the added info and get back to you here.

Doron
Typemock Support
answered by doron (17.2k points)
...