1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Core.Test/Framework/AutoMoq/AutoMoqerTest.cs

187 lines
4.3 KiB
C#
Raw Normal View History

2011-05-22 19:53:21 +03:00
// ReSharper disable RedundantUsingDirective
2011-06-18 05:00:44 +03:00
using System;
2011-04-08 09:50:30 +03:00
using AutoMoq;
using Moq;
2011-06-03 00:06:46 +03:00
using NUnit.Framework;
2011-04-08 09:50:30 +03:00
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class AutoMoqerTest
{
[Test]
public void GetMock_on_interface_returns_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var mock = mocker.GetMock<IDependency>();
//Assert
Assert.IsNotNull(mock);
}
[Test]
public void GetMock_on_concrete_returns_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var mock = mocker.GetMock<ConcreteClass>();
//Assert
Assert.IsNotNull(mock);
}
[Test]
public void Resolve_doesnt_return_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<ConcreteClass>().Do();
//Assert
Assert.AreEqual("hello", result);
}
[Test]
public void Resolve_with_dependency_doesnt_return_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<VirtualDependency>().VirtualMethod();
//Assert
Assert.AreEqual("hello", result);
}
[Test]
public void Resolve_with_mocked_dependency_uses_mock()
{
//Arrange
var mocker = new AutoMoqer();
mocker.GetMock<VirtualDependency>()
.Setup(m => m.VirtualMethod())
.Returns("mocked");
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().CallVirtualChild();
//Assert
Assert.AreEqual("mocked", result);
}
[Test]
public void Resolve_with_unbound_concerete_dependency_uses_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().CallVirtualChild();
var mockedResult = new Mock<VirtualDependency>().Object.VirtualMethod();
//Assert
Assert.AreEqual(mockedResult, result);
}
[Test]
public void Resolve_with_constant_concerete_dependency_uses_constant()
{
//Arrange
var mocker = new AutoMoqer();
var constant = new VirtualDependency { PropValue = Guid.NewGuid().ToString() };
2011-04-08 09:50:30 +03:00
mocker.SetConstant(constant);
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().GetVirtualProperty();
//Assert
Assert.AreEqual(constant.PropValue, result);
}
}
public class ConcreteClass
{
public string Do()
{
return "hello";
}
}
2011-04-10 05:44:01 +03:00
public class Dependency : IDependency
{
}
2011-04-08 09:50:30 +03:00
2011-04-10 05:44:01 +03:00
public interface IDependency
{
}
2011-04-08 09:50:30 +03:00
public class ClassWithDependencies
{
public ClassWithDependencies(IDependency dependency)
{
Dependency = dependency;
}
2011-04-10 05:44:01 +03:00
public IDependency Dependency { get; set; }
2011-04-08 09:50:30 +03:00
}
public class ClassWithVirtualDependencies
{
private readonly VirtualDependency _virtualDependency;
public ClassWithVirtualDependencies(IDependency dependency, VirtualDependency virtualDependency)
{
_virtualDependency = virtualDependency;
Dependency = dependency;
}
2011-04-10 05:44:01 +03:00
public IDependency Dependency { get; set; }
2011-04-08 09:50:30 +03:00
public string CallVirtualChild()
{
return _virtualDependency.VirtualMethod();
}
public string GetVirtualProperty()
{
return _virtualDependency.PropValue;
}
}
public class VirtualDependency
{
private readonly IDependency _dependency;
2011-04-10 05:44:01 +03:00
public VirtualDependency()
{
}
2011-04-08 09:50:30 +03:00
public VirtualDependency(IDependency dependency)
{
_dependency = dependency;
}
2011-04-10 05:44:01 +03:00
public string PropValue { get; set; }
2011-04-08 09:50:30 +03:00
public virtual string VirtualMethod()
{
return "hello";
}
}
2011-04-10 05:44:01 +03:00
}