Concrete mocking is one of the advanced features supported in Telerik JustMock. Up to this point we have been talking mostly about mocking interfaces. This feature allows you to mock the creation of an object.
To some extent this is available in the free edition and there are more things you can do in the commercial edition of the product. In this topic we will go through some examples demonstrating these differences.
Telerik JustMock also gives you the ability to explicitly specify whether a constructor should be mocked or not.
Note |
|---|
Refer to this topic to learn more about mocking internal types via proxy. |
In the further examples we will use the following sample class:
| C# | Copy |
|---|
public class FooVirtual
{
public FooVirtual()
{
throw new NotImplementedException( "Constructor" );
}
public virtual string Name
{
get;
set;
}
public virtual void VoidMethod()
{
throw new NotImplementedException();
}
public virtual IList<int> GetList()
{
throw new NotImplementedException();
}
}
|
Note that we have declared a concrete class FooVirtual which does not implement any interfaces.
Mocking concrete classes with virtual methods
What we need to do is to make sure that all class members are virtual. As you will see in the later examples there is a way to overcome this restriction. Here is the example code:
Important |
|---|
In the next example is used NUnit Testing Framework. |
| C# | Copy |
|---|
[TestMethod]
public void Should_Mock_Constructor()
{
var foo = Mock.Create<FooVirtual>(Constructor.Mocked);
foo.Arrange( x => x.GetList() ).CallOriginal().Occurs( 1 );
Assert.Throws<NotImplementedException>( () => foo.GetList() );
foo.Assert();
}
|
When using Maock.Create to create your mocked instance of a specific class you can specify whether or not the constructor should be mocked.
You can choose from the Constructor enumeration:
By default the constructor is not mocked.
Note that in the Arrange part of this example we use the CallOriginal and Occurs methods. This example shows how Telerik JustMock allows us to add behaviour checking - it will call the original GetList() method, verififing that the method call is made only once.
Concrete classes advanced mocking
This feature allows you to mock concrete objects without having to change anything in their interface.
Note |
|---|
This feature is available only in the commercial version of Telerik JustMock. Refer to this topic to learn more about the differences between both the commercial and free versions of Telerik JustMock. |
Here is the example class we are going to use:
| C# | Copy |
|---|
public class Foo
{
public Foo()
{
throw new NotImplementedException( "Constructor" );
}
public string Name
{
get;
set;
}
public void VoidMethod()
{
throw new NotImplementedException();
}
public IList<int> GetList()
{
throw new NotImplementedException();
}
}
|
Note that here we are going to mock an instance of the Foo class in the same way as we did for FooVirtual while Foo does not need to have all its members declared as virtual.
Important |
|---|
In the next example is used NUnit Testing Framework. |
| C# | Copy |
|---|
[TestMethod]
public void Should_Mock_Constructor()
{
var foo = Mock.Create<Foo>(Constructor.Mocked);
foo.Arrange( x => x.GetList() ).CallOriginal().Occurs( 1 );
Assert.Throws<NotImplementedException>( () => foo.GetList() );
foo.Assert();
}
|
See Also