MethodCallTests.cs :  » Testing » MockObjects » DotNetMock » Tests » Dynamic » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » MockObjects 
MockObjects » DotNetMock » Tests » Dynamic » MethodCallTests.cs
#region License
// Copyright (c) 2004 Griffin Caprio, Roman V. Gavrilov & Choy Rim. All rights reserved.
#endregion
#region Imports
using System;
using System.Reflection;

using NUnit.Framework;

using DotNetMock.Dynamic;
#endregion

namespace DotNetMock.Tests.Dynamic{
  /// <summary>
  /// Unit tests for <see cref="MethodCall"/>.
  /// </summary>
  [TestFixture]
  public class MethodCallTests 
  {
    interface IMethods 
    {
      void Method3(int x, string y, double z);
      void Method4();
      void Method4(int a);

      int Property0 { get; set; }

      void Method5(ToStringThrows x);

      int get_method6();
      void set_method6();
    }
    interface IMethodsA 
    {
      void Method3(int x, string y, double z);
    }
    class ToStringThrows
    {
      public override string ToString()
      {
        throw new Exception();
      }

    }

    static readonly MethodInfo method3 = typeof(IMethods).GetMethod("Method3");
    static readonly MethodInfo method4 = typeof(IMethods).GetMethod("Method4", new Type[0]);
    static readonly MethodInfo methodA3 = typeof(IMethodsA).GetMethod("Method3");
    static readonly MethodInfo method4a =
      typeof(IMethods).GetMethod(
      "Method4",
      new Type[] { typeof(int) }
      );
    static readonly MethodInfo property0_get = typeof(IMethods).GetMethod("get_Property0");
    static readonly MethodInfo property0_set = typeof(IMethods).GetMethod("set_Property0");
    static readonly MethodInfo method5 = typeof(IMethods).GetMethod("Method5");
    static readonly MethodInfo get_method6 = typeof(IMethods).GetMethod("get_method6");
    static readonly MethodInfo set_method6 = typeof(IMethods).GetMethod("set_method6");

    [Test] public void MethodNameWorksForMethodsWithNamesLikePropertyAccessors() 
    {
      MethodCall mc = new MethodCall(get_method6);
      Assert.IsFalse(mc.IsPropertyAccess);
      Assert.AreEqual("get_method6", mc.MethodName);
      mc = new MethodCall(set_method6);
      Assert.IsFalse(mc.IsPropertyAccess);
      Assert.AreEqual("set_method6", mc.MethodName);
    }
    [Test] public void MethodNameStripsPrefixOnProperties() 
    {
      MethodCall gc = new MethodCall(property0_get);
      Assert.IsTrue(gc.IsPropertyAccess);
      Assert.AreEqual("Property0", gc.MethodName);
      MethodCall sc = new MethodCall(property0_set, 1);
      Assert.IsTrue(sc.IsPropertyAccess);
      Assert.AreEqual("Property0", sc.MethodName);
    }

    [Test] public void MethodCallToString() 
    {
      MethodCall mc = new MethodCall(method3, 1, "two", 3.4);
      Assert.AreEqual("Method3(x=1, y=\"two\", z="+3.4+")", mc.ToString());
      mc = new MethodCall(method4);
      Assert.AreEqual("Method4()", mc.ToString());
      mc = new MethodCall(method5, new ToStringThrows());
      Assert.AreEqual("Method5(x=N/A)", mc.ToString());
    }
    [Test] public void MethodCallEqualsMethodCall() 
    {
      MethodCall mc1 = new MethodCall(method3, 1, "two", 3.4);
      MethodCall mc2 = new MethodCall(method3, 1, "two", 3.4);
      MethodCall mc3 = new MethodCall(method3, 1, "2", 3.4);
      MethodCall mc4 = new MethodCall(method4);
      MethodCall mc5 = new MethodCall(methodA3, 1, "two", 3.4);
      MethodCall mc6 = new MethodCall(method4a, 1);

      Assert.AreEqual(mc1, mc2);
      Assert.IsFalse(mc1.Equals(mc3));
      Assert.IsFalse(mc1.Equals(mc4));
      Assert.IsFalse(mc1.Equals(mc5));
      Assert.IsFalse(mc1.Equals(null), "MethodCall not equal to null");
      Assert.IsFalse(mc1.Equals("text"), "MethodCall not equal to string");
      Assert.IsFalse(mc4.Equals(mc6), "overloaded methods don't match");
    }
    [ExpectedException(
       typeof(InvalidOperationException),
       "Method Method4 takes 0 arguments but received 1."
       )]
    [Test] public void TooManyArguments() 
    {
      MethodCall mc = new MethodCall(method4, 1);
    }
    [ExpectedException(
       typeof(InvalidOperationException),
       "Method Method3 takes 3 arguments but received 1."
       )]
    [Test] public void TooFewArguments() 
    {
      MethodCall mc = new MethodCall(method3, 1);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.