Is there any example how unit test can be written for manipulation with items in Tridion using Core Service?

For example, we would like to have test for Workflow, event system, creating and updating component in Tridion...

How to mock items and write real unit test?


The answer is yes and no.

You can write unit test case for the code using core service however you can't write unit test cases for event system implementation unless you are using core service for event system implementation.

AFAIK Event system uses TOM.Net api and which doesn't have unit testable implementation. Because when you create page mock object for page and a method is expecting page object then "(mock object).object" call will actually invoke database connection and will try to connect to database which is not desirable for unit testing.

Sample code for unit testing using core service is to create an interface of all the methods that you want to be executed then use following code: In the example given below getdata is the method in interface which is actually invoking core service read method.

Mock<ICoreServiceHelper> _coreServiceHelper = new Mock<ICoreServiceHelper>();
var _schemaData = new SchemaData
                Id = "tcm:X-XXXX-8",
                Title = "TestSchema",
                Description = "Testing",
                NamespaceUri = "Test",
                RootElementName = "Content"

_coreServiceHelper.Setup(x => x.GetData(It.IsAny<string>())).Returns(_schemaData);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.