|
using System;
using Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestCases
{
[TestClass]
public class Tests
{
[TestInitialize]
public void ClearApparatus()
{
TestApparatus.Reset();
}
[TestCleanup]
public void CleanupApparatus()
{
TestApparatus.VerifyAccess();
}
[TestMethod]
[Description("The test will fail upon calling the static method")]
[ExpectedException(typeof(NotImplementedException))]
public void FailedOnStaticProvider()
{
SampleObject sample = new SampleObject();
sample.Methods();
}
[TestMethod]
[Description("With only static method mocked, the test will still fail")]
[ExpectedException(typeof(NotImplementedException))]
public void FailedOnInstancedProvider()
{
// Mock the static method
TestApparatus.MockStatic<bool, int>(StaticProvider.Provide).If(input => true).Return(true);
SampleObject sample = new SampleObject();
sample.Methods();
}
[TestMethod]
[Description("With both methods mocked, the test should pass")]
public void AllSuccess()
{
// Mock both the static method and instance method
TestApparatus.MockStatic<bool, int>(StaticProvider.Provide).Any().Return(true);
TestApparatus.MockInstance<InstancedProvider, bool, int>(i => i.Provide).Expecting(1).If(a1 => a1 >= 0).Return(true);
SampleObject sample = new SampleObject();
sample.Methods();
}
[TestMethod]
[Description("Without property mocking")]
public void WithoutMockingProperty()
{
SampleObject sample = new SampleObject();
Assert.AreEqual(SampleObject.TestingValue, sample.Values());
}
[TestMethod]
[Description("Mocking a specific property")]
public void MockingProperty()
{
const int mocked = 12034;
TestApparatus.MockProperty(() => StaticProvider.Value).Any().Return(mocked);
TestApparatus.MockProperty((InstancedProvider i) => i.Value).Expecting(1).Any().Return(mocked);
SampleObject sample = new SampleObject();
Assert.AreEqual(mocked, sample.Values());
}
[TestMethod]
[Description("Mocking a property with some condition")]
public void MockingPropertyConditionally()
{
int threshold = 0;
TestApparatus.MockProperty((InstancedProvider i) => i.Value).If(i => threshold > 1000).Return(5);
SampleObject sample = new SampleObject();
threshold = 10;
Assert.AreEqual(SampleObject.TestingValue, sample.Values());
threshold = 10000;
Assert.AreEqual(5, sample.Values());
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.