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 All,

Newbie question,
I've chain "HttpContext.Current.Request.Url.AbsoluteUri" that should return "http://localhost". It works fine when recorded as:
recorder.ExpectAndReturn(
HttpContext.Current.Request.Url.AbsoluteUri,
@"http://localhost");

But, I need RepeatAlways functionallity for the whole chain. As I found RepeatAlways works only for last call, so it will work only for Url.AbsoluteUri. As I found I can use recorder.DefaultBehavior.RepeatAlways but how it will work exactly. Its not clear from documentation. What are side effects from using 'recorder.DefaultBehavior.*'. Will it applied for my chain only or to all recodred calls?

Thanks.
asked by tom3m (9.7k points)

3 Answers

0 votes
Hi
recorder.DefaultBehavior.RepeatAlways will apply to ALL calls
including chained statements.
From TypeMock documentation
Without using the default repeat, only the last method is repeated; use Default to repeat chained statements.


Example:
class TheClass
{       
   public int SomeMethod()
   {
      return 1;
   }
}

public class TestClass
{
   [Test]
   public void Test()
   {
      using (RecordExpectations recorder = RecorderManager.StartRecording())
      {
         recorder.DefaultBehavior.RepeatAlways();
         string s = HttpContext.Current.Request.Url.AbsoluteUri;
         recorder.Return("http://localhost");
         
         TheClass mock = new TheClass();
         mock.SomeMethod();
         recorder.Return(5);
      }

      Assert.AreEqual("http://localhost", HttpContext.Current.Request.Url.AbsoluteUri);
      Assert.AreEqual("http://localhost", HttpContext.Current.Request.Url.AbsoluteUri);
      TheClass c = new TheClass();
      Assert.AreEqual(5, c.SomeMethod());
      Assert.AreEqual(5, c.SomeMethod());
   }
}


Hope it helps.
answered by ohad (35.4k points)
0 votes
Still not clear. As I understand default behaviour is Repeat(1), so, will my chain works as expected in the following case?

using (RecordExpectations recorder = new RecordExpectations())
{
<other mocks>

// I want mock chain, so I need change default behavior
recorder.DefaultBehavior.RepeatAlways();

recorder.ExpectAndReturn(
HttpContext.Current.Request.Url.AbsoluteUri,
@"http://localhost");

// I want return back default behavior, can I? Is that correct code?
recorder.DefaultBehavior.Repeat(1);

<other mocks>
}
answered by tom3m (9.7k points)
0 votes
Hi
Sure the solution you wrote will work.
You can always go back and forth with default behavior
as much as you need.
answered by ohad (35.4k points)
...