or
public class Bi
{
protected bool Equals(Bi other)
{
return I == other.I;
}
public override int GetHashCode()
{
return I;
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public int I { get; set; }
}
public interface IR
{
bool Get(Bi bi);
}
public class G
{
private readonly IR _r;
public G(IR r)
{
_r = r;
}
public bool M()
{
var bi = new Bi {I = 1};
return _r.Get(bi);
}
}
[TestFixture]
public class Test
{
[Test]
public void Test1()
{
var bi = new Bi {I = 1};
var r = Mock.Create<
IR
>();
Mock.Arrange(() => r.Get(bi)).Returns(true);
var g = new G(r);
var b = g.M();
Assert.That(b, Is.True);
}
}
var oRegMgr = Mock.Create<
IRegionManager
>();
var oCmdRegion = Mock.Create<
IRegion
>();
var oCmdView = Mock.Create<
CommandingView
>(Constructor.Mocked);
Mock.Arrange(() => oRegMgr.Regions[RegionNames.CommandingRegion]).Returns(oCmdRegion);
Mock.Arrange(() => oCmdRegion.ActiveViews.FirstOrDefault()).Returns(oCmdView);
IRegion commandingRegion = oRegionManager.Regions[RegionNames.CommandingRegion];
CommandingView oCmdView = (CommandingView)commandingRegion.ActiveViews.FirstOrDefault();
var oCmdView = Mock.Create<CommandingView>();
Hi, I'm trying to unit test a common interface used in a generic Repository, something like T GetWhere(Func<T, Boolean> where)...
When I set this expectation in a test
Mock.Arrange(() => bookRepo.GetWhere(book => book.Id == 1))
.Returns(expectedBook)
.MustBeCalled();
JustMock throws this exception: "Telerik.JustMock.MockAssertionException: Setup contains calls that are marked as "MustBeCalled" but actually never called"
It seems the GetWhere() method was never call, to return the expected object.
Below is a simple example that illustrate the case.
How should I rewrite the second test below so that I can mock and test these type of interfaces?
Thanks in advance.
using
System;
using
Microsoft.VisualStudio.TestTools.UnitTesting;
using
Telerik.JustMock;
//Free version 2011.1.331.1
using
Model.Infrastructure;
using
Model;
namespace
Model.Infrastructure
{
public
interface
IRepository<T> where T :
class
{
T GetById(
int
Id);
T GetWhere(Func<T, Boolean> where);
}
}
namespace
Model
{
public
class
Book
{
public
int
Id {
get
;
set
; }
public
string
BookTitle {
get
;
set
; }
public
string
OnLoanTo {
get
;
set
; }
public
Book(){}
}
public
class
BookService
{
private
IRepository<Book> _bookRepository;
public
BookService(IRepository<Book> bookRepository)
{
this
._bookRepository = bookRepository;
}
public
Book GetSingleBook1(
int
bookId)
{
return
_bookRepository.GetById(bookId);;
}
public
Book GetSingleBook2(
int
bookId)
{
Book b = _bookRepository.GetWhere(x => x.Id == bookId);
return
b;
}
}
}
namespace
Model.Tests
{
[TestClass]
public
class
AssortedTests
{
private
Book expectedBook;
private
IRepository<Book> bookRepo;
private
BookService bookService;
[TestInitialize]
public
void
Setup()
{
expectedBook =
new
Book()
{ Id = 1,
BookTitle =
"Adventures"
,
OnLoanTo =
"John Smith"
};
bookRepo = Mock.Create<IRepository<Book>>();
bookService =
new
BookService(bookRepo);
}
[TestMethod]
public
void
Book_CanRetrieveBookFromRepository()
{
Mock.Arrange(() => bookRepo.GetById(1))
.Returns(expectedBook)
.MustBeCalled();
//Act
Book actualBook = bookService.GetSingleBook1(1);
Mock.Assert(bookRepo); //Pass...
Assert.IsTrue(actualBook.BookTitle ==
"Adventures"
);
}
[TestMethod]
public
void
Book_CanRetrieverFromRepositoryUsingLambdaExpressions()
{
Mock.Arrange(() => bookRepo.GetWhere(book => book.Id == 1))
.Returns(expectedBook)
.MustBeCalled();
//Act
Book actualBook = bookService.GetSingleBook2(1);
Mock.Assert(bookRepo);
//Throws the error...
Assert.IsTrue(actualBook.BookTitle ==
"Adventures"
);
}
}
}
Mock.SetupStatic(typeof(FormsAuthentication));
Mock.Arrange(() => FormsAuthentication.SignOut()).OccursOnce();
var result = lc.Logoff();
Mock.Assert(() => FormsAuthentication.SignOut());
Test Name: LogoffShouldCallFormsAuthenticationSignOut
Test FullName: OfDisplays.WebTests.SecurityTests.LogoffShouldCallFormsAuthenticationSignOut
Test Source: c:\SoftDev\Projects\OFDisplays\OfDisplays.Web\OfDisplays.WebTests\SecurityTests.cs : line 83
Test Outcome: Failed
Test Duration: 0:00:01.9634171
Result Message:
Test method OfDisplays.WebTests.SecurityTests.LogoffShouldCallFormsAuthenticationSignOut threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'Telerik.CodeWeaver.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Result StackTrace:
at System.Web.Security.FormsAuthentication.SignOut()
at OfDisplays.Web.Controllers.LoginController.Logoff() in c:\SoftDev\Projects\OFDisplays\OfDisplays.Web\OfDisplays.Web\Controllers\LoginController.cs:line 57
at OfDisplays.WebTests.SecurityTests.LogoffShouldCallFormsAuthenticationSignOut() in c:\SoftDev\Projects\OFDisplays\OfDisplays.Web\OfDisplays.WebTests\SecurityTests.cs:line 90
public partial class portailSOAPClient : System.ServiceModel.ClientBase<FiduPortail.portailSOAP>, FiduPortail.portailSOAP {}Do you know how to resolve this issue.
MESSAGE: Test method STB.EP.RBS.Test.TransportBooking.DriverComponentTest.testCheckExistDriver threw exception: Telerik.JustMock.MockException: Profiler must be enabled to mock/assert target SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated) method. +++++++++++++++++++ STACK TRACE: at Telerik.JustMock.Handlers.InterceptorHandler.Create(Object target, MethodInfo methodInfo, Boolean privateMethod) at Telerik.JustMock.MockContext`1.SetupMock(MockExpression`1 expression) at Telerik.JustMock.Mock.<>c__DisplayClassd.<Arrange>b__c(MockContext`1 x) at Telerik.JustMock.MockContext.Setup[TDelgate,TReturn](Instruction instruction, Func`2 function) at Telerik.JustMock.Mock.Arrange(Expression`1 expression) at STB.EP.RBS.Test.TransportBooking.DriverComponentTest.testCheckExistDriver() in C:\Project\STB\RBS\Tests\STB.EP.RBS.Test\TransportBooking\DriverComponentTest.cs:line 58 Thanks! Regards Suyi