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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I am new to TypeMock, and would like to setup a small infrastructure to reuse Expectations in multiples tests. Running "test1", I get a mock ScenarioGeneral. The "Test2" method is not mock, why and how to reuse expectations?

Any help would be greatly appreciated.

using System;
using NUnit.Framework;
using TypeMock;

namespace TypeMockTest
{
    [TestFixture]
    public class DalMock
    {
        const long SCENARIO_ID = 123;          

        ScenarioGeneral scenario;

        [SetUp]
        public void SetUp()
        {
            MockManager.Init();
            scenario = new Scenario(SCENARIO_ID);
        }


        [TearDown]
        public void Tear()
        {
            MockManager.Verify();
        }


        [Test]
        public void Test1()
        {
            InitMocks();
            Scenario testScenario = Factory.Current.SelectScenario(SCENARIO_ID);
        }

        [Test]
        public void Test2()
        {
            InitMocks();
            Scenario testScenario = Factory.Current.SelectScenario(SCENARIO_ID);
        }


        public void InitMocks()
        {
            using (RecordExpectations recorder = new RecordExpectations())
            {
                // Mock factor
                Factory factory = new Factory();

                // Mock factory SelectScenario
                factory.SelectScenario(SCENARIO_ID);
                recorder.Return(scenario).RepeatAlways();
            }
        }
    }
}
[/quote]
asked by laurend (600 points)

1 Answer

0 votes
Hi
It seems like the problem is that The Current property is not mocked.
Actually what is strange to me is that Test1 method passed.
We will look into this and I'll get back to you with an answer.

Meanwhile try this:
Using natural mocks you can mock chains. You should use
recorder.DefaultBehavior.RepeatAlways() when you want to repeat all the chain. (The Repeat() method handles only the last part of the chain)
So try this:
public void InitMocks()
{
   using (RecordExpectations recorder = new RecordExpectations())
   {
      // Mock factor    
      recorder.DefaultBehavior.RepeatAlways();
      Factory.Current.SelectScenario(SCENARIO_ID);
      recorder.Return(scenario);
   }
}


Hope it helps. :)
answered by ohad (35.4k points)
...