Unit testing with Model Builder
# help-with-umbraco
t
Hi I've been through the links on Umbraco 13 to setup some unit tests. I'm finding it incredibly difficult in how I can create a mock model to test this model with some logic as most of them are marked as readonly? Any help would be appreciated
l
I'd recommend going with the integrated database option. Use the builders to create your doctype structure, then use the ContentService to actually publish something. After that you can just query it and get it as the proper model.
Another option is to do an actual usync import in your setup. 🙃
Bottom line, don't mock! 🙈
Ah, I forget. There's a builder for content too.
Copy code
csharp
            var content = new ContentBuilder()
                .WithoutIdentity()
                .WithName("Variant English")
                .WithCultureName(Cultures.EnIso, "English variant")
                .WithCultureName(Cultures.NbIso, "Norsk variant")
                .WithContentType(RootContentType)
                .AddPropertyData()
                .WithKeyValue("property", DataTypeTestCase.InputValue)
                .Done()
                .Build();

            ContentService.SaveAndPublish(content);
There's a final gotcha. You gotta go builder.AddNucache() in CustomTestSetup. Otherwise all good. 🙃
5 Views