or
public
class
BackgroundThread
{
private
Widget _widget;
public
BackgroundThread(Widget widget)
{
_widget = widget;
}
public
void
Run()
{
Console.WriteLine(
"widget="
+_widget);
_widget.DoSomething();
}
}
public
class
Widget
{
public
void
DoSomething()
{
throw
new
Exception(
"Not ready to do something"
);
}
}
// Test Class
[TestMethod]
public
void
TestBackgroundThread()
{
var widget = Mock.Create<Widget>();
var bt =
new
BackgroundThread(widget);
var thread =
new
Thread(bt.Run);
thread.Start();
Console.WriteLine(
"Thread started"
);
thread.Join();
// Exception is thrown but should not because object was mocked
}
public class Test1
{
protected void Testing1()
{
string s = "Testing1";
throw new Exception("shouldn't be here");
}
}
public class Test2 : Test1
{
protected void Testing2()
{
Testing1();
string s = "Testing2";
throw new Exception("shouldn't be here");
}
}
public class Test3 : Test2
{
protected void Testing3()
{
Testing1();
Testing2();
string s = "Testing3";
}
}
[Test]
public void MakeSureAllMethodsAreMockedOut()
{
Test3 test3 = Mock.Create<
Test3
>();
Mock.NonPublic.Arrange(test3, "Testing3").CallOriginal().MustBeCalled();
test3.GetType().GetMethod("Testing3", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).Invoke(test3, new object[] { });
}
public class Test3
{
protected void Testing1()
{
string s = "Testing1";
throw new Exception("shouldn't be here");
}
public void Testing3()
{
Testing1();
string s = "Testing3";
}
}
public static IRule MakeMockRule(string description, string rulename, string propname, RuleSeverity severity, bool isbroke)
{
IRule mockRule = Mock.Create<
IRule
>();
Mock.Arrange(() => mockRule.PropertyName).Returns(propname);
Mock.Arrange(() => mockRule.RuleName).Returns(rulename);
Mock.Arrange(() => mockRule.Severity).Returns(severity);
Mock.Arrange(() => mockRule.IsBroken).Returns(isbroke);
Mock.Arrange(() => mockRule.HandlesProperty(propname)).Returns(true);
Mock.Arrange(() => mockRule.Description).Returns(description);
return mockRule;
}
var source = Mock.Create<FooBase>(Constructor.Mocked);
private class _commandLineArgument
), everything works as expected. Anybody can explain this behavior? Anyway to make it work without making it a class. Thank you.public abstract class ConsoleBase
{
private struct _commandLineArgument
{
public string Name { get; set; }
public string Description { get; set; }
public bool IsRequired { get; set; }
public string Value { get; set; }
public bool Found { get; set; }
}
}
Type argType = Type.GetType( "Utilities.ConsoleBase+_commandLineArgument" );
Mock.NonPublic.Arrange<
bool
>( argType, "Found" ).Returns(true);
using
(var siteCollection =
new
SPSite(spWebAppUri + siteUrl))
{
// more code...
}
// the next time a new instance of SPSite is created, use our fake one instead
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
IRTObjectModel model = Mock.Create<IRTObjectModel>(); Mock.Arrange<TimeSpan>(() => model.ShiftDateStartTime) .Returns(shiftDateStartTime); Mock.Arrange<IDictionary<string, FuelBay>>(() => model.FuelBays) .Returns(initialTestData.ActiveFuelBays); Mock.Arrange<IDictionary<string, FuelTruck>>(() => model.FuelTrucks) .Returns(initialTestData.ActiveFuelTrucks);
using
System.Diagnostics;
[ClassInitialize()]
public
static
void
MyClassInitialize(
TestContext testContext)
{
Mock.Initialize(
typeof
(Debug));
Mock.Partial(
typeof
(Debug)).For<
bool
,
string
>((i, j) => Debug.Assert(i, j));
}
public
void
Test()
{
Mock.Arrange(() => Debug.Assert(Arg.AnyBool, Arg.AnyString)).DoNothing();
DoSomeFunctionThatAsserts();
}
public
class
UnmockableClass<T>
{
private
bool
_dummyvariable;
public
UnmockableClass()
{
_dummyvariable =
true
;
}
public
static
UnmockableClass<T> GetInstance<TDescriptor>() where TDescriptor : IDescriptor<T>
{
throw
new
NotImplementedException();
}
}
[TestClass]
public
class
UnmockableClassTest
{
[TestMethod]
public
void
SimpleTest()
{
Mock.SetupStatic<UnmockableClass<AvailableID>>();
//nothing else. The test will fail with just this line of code above
}
}
Test 'JustMockIssueTestMock.UnmockableClassTest.SimpleTest' failed: Test method JustMockIssueTestMock.UnmockableClassTest.SimpleTest threw exception:
System.ArgumentException: GenericArguments[0], 'JustMockIssue.IDescriptor`1[T]', on 'JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID] GetInstance[TDescriptor]()' violates the constraint of type 'TDescriptor'. ---> System.Security.VerificationException: Method JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID].GetInstance: type argument 'JustMockIssue.IDescriptor`1[T]' violates the constraint of type parameter 'TDescriptor'.
at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
--- End of inner exception stack trace ---
at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
at Telerik.JustMock.Weaver.WeaverInterceptorBuilder.DefineInterceptor(MethodBase methodBase, FieldBuilder fldInterceptor, Type[] parameters)
at Telerik.JustMock.Weaver.DynamicInjector.EmitInterceptor(ILGenerator il, MethodBase methodInfo)
at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase, MethodInfo containerMethodInfo)
at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase, MethodInfo injectingMethod, Boolean force)
at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase)
at Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static)
at Telerik.JustMock.MockManager.SetupStatic()
at Telerik.JustMock.Mock.SetupStatic(Type targetType, Behavior behavior, StaticConstructor staticConstructor)
at Telerik.JustMock.Mock.SetupStatic[T]()
UnmockableClassTest.cs(17,0): at JustMockIssueTestMock.UnmockableClassTest.SimpleTest()
[Test]
public
void
datetime_test_justmock()
{
var YEAR_2K =
new
DateTime(2000, 1, 1);
Mock.Arrange(() => DateTime.Now).Returns(YEAR_2K);
var actual = DateTime.Now;
Assert.AreEqual(YEAR_2K, actual);
}