+ Reply to Thread
Results 1 to 2 of 2

Thread: Testing mocked objects

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    28

    Testing mocked objects

    Hi , i am using RhinoMocks and I am new at it, actually i am writing a test as shown :
    Code:
    public class A
    {
        public void methodA(){}
    }
    public class B
    {
        public void methodB(A a)
        {
          a.methodA();
        }
    }
    And i am trying to test it like this
    Code:
    A a = MockRepository.GenerateMock<A>();
    public void ShouldTest()
    {
        B b = new B();
        b.methodB(a);
        a.AssertWasCalled(x=>x.methodA());
        a.VerifyAllExpectations();
    }
    But it is giving the error as shown:
    Code:
    System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    40
    You can do two things
    1) Create an interface
    2) Make the member virtual

    Here is a code sample :
    Code:
    public interface IA { void methodA();}
    public class A:IA{public void methodA() { }}
    public class B
    {
        public void methodB(IA a)
        {
            a.methodA();
        }
    }
    
    [TestFixture]
    public class Bar
    {
        [Test]
        public void BarTest()
        {
    
            var repo = MockRepository.GenerateMock<IA>();
    
            B b = new B();
            b.methodB(repo);
    
            repo.AssertWasCalled(a => a.methodA());
            repo.VerifyAllExpectations();
        }
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts