How do you mock UnitOfWork by Rhino.Commons?

My application uses Rhino.Commons - NHRepository and UnitOfWork. I like the syntax of With.Transaction () for transactions and has been used for some time.

But I got a problem - how do I mock UnitOfWork for testing? This is especially causing me problems:

With.Transaction(() => Repositories.TwinfieldSpooler.Update(spool));

I can mock repositories with Rhino.Mocks, but how can I easily mock UnitOfWork for this type of code?

+3
source share
4 answers

.Transaction UnitOfWork.Current. UnitOfWork - - RhinoMocks.

UnitOfWork.Current , . , .

3 :

  • Rhino.Commons, UnitOfWork.Current setter public unit test.

  • , UnitOfWork.Current .

  • UnitOfWork.Current Local.Data, , :

    Rhino.Commons.Local.Data [UnitOfWork.CurrentUnitOfWorkKey] = myFakeUnitOfWork;

- , UnitOfWork.Current - IUnitOfWork, RhinoMocks .

, , Rhino.Commons, , , UnitOfWork. , Rhino.

+3

, , . , / UnitOfWork, 2 SetUp :

IUnitOfWork theStubUnitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
UnitOfWork.RegisterGlobalUnitOfWork(theStubUnitOfWork);
+3
source

Just extra info for others: Ogre Psalm33 mentioned how to drown UnitOfWork, but if you also want to work with .Transaction, you can optionally drown RhinoTransaction:

IUnitOfWork stubUnitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
RhinoTransaction stubTx = MockRepository.GenerateStub<RhinoTransaction>();
stubUnitOfWork.Expect(x => x.BeginTransaction(System.Data.IsolationLevel.Unspecified)).IgnoreArguments().Return(stubTx);
UnitOfWork.RegisterGlobalUnitOfWork(stubUnitOfWork);

I remember seeing a note from Ayende somewhere that he usually used With.Transaction only as a last resort, when he could not use his preferred Lock automatic transaction Management .

0
source

Source: https://habr.com/ru/post/1706022/


All Articles