Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Created May 21, 2016 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urasandesu/a1f3c0f337483362b026b25f0d1c2125 to your computer and use it in GitHub Desktop.
Save urasandesu/a1f3c0f337483362b026b25f0d1c2125 to your computer and use it in GitHub Desktop.
Example for mocking the instance method of a struct. I added the feature to Prig because I knew that Microsoft Fakes doesn't support [it](https://social.msdn.microsoft.com/Forums/en-US/3be5abc6-1b21-4322-a1f6-9573d38c4885/can-we-use-fakes-to-test-automationelement-methods?forum=vsunittest).
using System;
namespace StructInstanceMethodSample
{
public class DateTimeHelper
{
public static DateTime ConvertServerTimeToTimeZoneTime(DateTime inputDateTime, string zoneId, bool __no_use__)
{
// dummy implementation
var utcDateTime = inputDateTime.ToUniversalTime();
return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.FindSystemTimeZoneById(zoneId));
}
}
}
using NUnit.Framework;
using StructInstanceMethodSample;
using System;
using System.Prig;
using Urasandesu.Prig.Framework;
namespace StructInstanceMethodSampleTest
{
[TestFixture]
public class DateTimeHelperTest
{
[Test]
public void ConvertServerTimeToTimeZoneTimeTest()
{
using (new IndirectionsContext())
{
// Arrange
PDateTime.ToUniversalTime().Body = (ref DateTime @this) => @this.AddHours(-10);
PTimeZoneInfo.ConvertTimeFromUtcDateTimeTimeZoneInfo().Body = (dateTime, destinationTimeZone) => dateTime.AddHours(+8);
// Act
var inputDateTime = DateTime.Now;
var timeZoneTime = DateTimeHelper.ConvertServerTimeToTimeZoneTime(inputDateTime, "China Standard Time", true);
// Assert
var diff = timeZoneTime - inputDateTime;
Assert.AreEqual(-2, diff.Hours);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="prig" type="Urasandesu.Prig.Framework.PilotStubberConfiguration.PrigSection, Urasandesu.Prig.Framework" />
</configSections>
<prig>
<stubs>
<!--
PDateTime.ToUniversalTime().Body =
(ref System.DateTime @this) =>
{
throw new NotImplementedException();
};
-->
<add name="ToUniversalTime" alias="ToUniversalTime">
<RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assembly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
<Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">ToUniversalTime</Name>
<AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
<ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime</ClassName>
<Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime ToUniversalTime()</Signature>
<Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime ToUniversalTime()</Signature2>
<MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
<GenericArguments i:nil="true" xmlns="" />
</RuntimeMethodInfo>
</add>
<!--
PTimeZoneInfo.ConvertTimeFromUtcDateTimeTimeZoneInfo().Body =
(dateTime, destinationTimeZone) =>
{
throw new NotImplementedException();
};
-->
<add name="ConvertTimeFromUtcDateTimeTimeZoneInfo" alias="ConvertTimeFromUtcDateTimeTimeZoneInfo">
<RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assembly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
<Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">ConvertTimeFromUtc</Name>
<AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
<ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.TimeZoneInfo</ClassName>
<Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)</Signature>
<Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)</Signature2>
<MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
<GenericArguments i:nil="true" xmlns="" />
</RuntimeMethodInfo>
</add>
</stubs>
</prig>
</configuration>
@urasandesu
Copy link
Author

Complete solution is available in my google drive:
https://drive.google.com/file/d/0B4s_gXgDJAgbd21CRHpsVlFtNTA/view?usp=sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment