Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dennis 16 posts 170 karma points
    1 week ago
    Dennis
    0

    Having some difficulty with automated testing and umbraco 8

    Hi, I am trying to learn about unit- and integration testing with umbraco 8. I have stumbled upon an issue though that I simply cannot explain or understand. I watched this video on youtube about automated testing with umbraco 8 and the video recommended to use our.umbraco.community.tests package, so I have it currently installed, but it doesn't seem to work the way I expected it to.

    The problem: I am unable to use the Mapper property, inherited from the UmbracoTestBase class. When I try to access it, I get an error saying:

      Message: 
    System.NullReferenceException : Object reference not set to an instance of an object.
    
      Stack Trace: 
    ServiceContainer.EmitLifetime(ServiceRegistration serviceRegistration, Action`1 emitMethod, IEmitter emitter) line 4660
    <>c__DisplayClass197_0.<ResolveEmitMethod>b__1(IEmitter methodSkeleton) line 4649
    <>c__DisplayClass153_0.<CreateEmitMethodWrapper>b__0(IEmitter ms) line 3856
    ...
    

    This message means nothing to me, I don't know what I am missing. In an attempt to fix it, I cloned the source code of umbraco 8 and checked out this unit test class that tests the mapper. I ran the test in the umbraco source code and it ran succesful, so it works. I then copied over the code in that class into my test project like this:

    [TestFixture]
    [UmbracoTest(WithApplication = true)]
    public class TestMapper : UmbracoTestBase
    {
        // mocks of services that can be setup on a test by test basis to return whatever we want
        private readonly Mock<IContentTypeService> _contentTypeService = new Mock<IContentTypeService>();
        private readonly Mock<IContentService> _contentService = new Mock<IContentService>();
        private readonly Mock<IDataTypeService> _dataTypeService = new Mock<IDataTypeService>();
        private readonly Mock<IEntityService> _entityService = new Mock<IEntityService>();
        private readonly Mock<IFileService> _fileService = new Mock<IFileService>();
        private Mock<PropertyEditorCollection> _editorsMock;
    
        public override void SetUp()
        {
            TestOptionAttributeBase.ScanAssemblies.Add(GetType().Assembly);
            base.SetUp();
        }
    
        protected override void Compose()
        {
            base.Compose();
    
            // create and register a fake property editor collection to return fake property editors
            var editors = new DataEditor[] { new TextboxPropertyEditor(Mock.Of<ILogger>()), };
            var dataEditors = new DataEditorCollection(editors);
            _editorsMock = new Mock<PropertyEditorCollection>(dataEditors);
            _editorsMock.Setup(x => x[It.IsAny<string>()]).Returns(editors[0]);
            Composition.RegisterUnique(f => _editorsMock.Object);
    
            Composition.RegisterUnique(_ => _contentTypeService.Object);
            Composition.RegisterUnique(_ => _contentService.Object);
            Composition.RegisterUnique(_ => _dataTypeService.Object);
            Composition.RegisterUnique(_ => _entityService.Object);
            Composition.RegisterUnique(_ => _fileService.Object);
        }
    
        [Test]
        public void test()
        {
            //Arrange
    
            // setup the mocks to return the data we want to test against...
    
            _dataTypeService.Setup(x => x.GetDataType(It.IsAny<int>()))
                .Returns(Mock.Of<IDataType>(
                    definition =>
                        definition.Id == 555
                        && definition.EditorAlias == "myPropertyType"
                        && definition.DatabaseType == ValueStorageType.Nvarchar));
    
            var display = CreateMemberTypeSave();
    
            //Act
    
            var result = Mapper.Map<IMemberType>(display);
    
            //Assert
    
            Assert.AreEqual(display.Alias, result.Alias);
            Assert.AreEqual(display.Description, result.Description);
            Assert.AreEqual(display.Icon, result.Icon);
            Assert.AreEqual(display.Id, result.Id);
            Assert.AreEqual(display.Name, result.Name);
            Assert.AreEqual(display.ParentId, result.ParentId);
            Assert.AreEqual(display.Path, result.Path);
            Assert.AreEqual(display.Thumbnail, result.Thumbnail);
            Assert.AreEqual(display.IsContainer, result.IsContainer);
            Assert.AreEqual(display.AllowAsRoot, result.AllowedAsRoot);
            Assert.AreEqual(display.CreateDate, result.CreateDate);
            Assert.AreEqual(display.UpdateDate, result.UpdateDate);
    
            // TODO: Now we need to assert all of the more complicated parts
            Assert.AreEqual(display.Groups.Count(), result.PropertyGroups.Count);
            for (var i = 0; i < display.Groups.Count(); i++)
            {
                Assert.AreEqual(display.Groups.ElementAt(i).Id, result.PropertyGroups.ElementAt(i).Id);
                Assert.AreEqual(display.Groups.ElementAt(i).Name, result.PropertyGroups.ElementAt(i).Name);
                var propTypes = display.Groups.ElementAt(i).Properties;
                Assert.AreEqual(propTypes.Count(), result.PropertyTypes.Count());
                for (var j = 0; j < propTypes.Count(); j++)
                {
                    Assert.AreEqual(propTypes.ElementAt(j).Id, result.PropertyTypes.ElementAt(j).Id);
                    Assert.AreEqual(propTypes.ElementAt(j).DataTypeId, result.PropertyTypes.ElementAt(j).DataTypeId);
                    Assert.AreEqual(propTypes.ElementAt(j).MemberCanViewProperty, result.MemberCanViewProperty(result.PropertyTypes.ElementAt(j).Alias));
                    Assert.AreEqual(propTypes.ElementAt(j).MemberCanEditProperty, result.MemberCanEditProperty(result.PropertyTypes.ElementAt(j).Alias));
                    Assert.AreEqual(propTypes.ElementAt(j).IsSensitiveData, result.IsSensitiveProperty(result.PropertyTypes.ElementAt(j).Alias));
                }
            }
    
            Assert.AreEqual(display.AllowedContentTypes.Count(), result.AllowedContentTypes.Count());
            for (var i = 0; i < display.AllowedContentTypes.Count(); i++)
            {
                Assert.AreEqual(display.AllowedContentTypes.ElementAt(i), result.AllowedContentTypes.ElementAt(i).Id.Value);
            }
        }
        private MemberTypeSave CreateMemberTypeSave()
        {
            return new MemberTypeSave
            {
                Alias = "test",
                AllowAsRoot = true,
                AllowedContentTypes = new[] { 666, 667 },
                Description = "hello world",
                Icon = "tree-icon",
                Id = 1234,
                Key = new Guid("8A60656B-3866-46AB-824A-48AE85083070"),
                Name = "My content type",
                Path = "-1,1234",
                ParentId = -1,
                Thumbnail = "tree-thumb",
                IsContainer = true,
                Groups = new[]
                {
                    new PropertyGroupBasic<MemberPropertyTypeBasic>()
                    {
                        Id = 987,
                        Name = "Tab 1",
                        Alias = "tab1",
                        SortOrder = 0,
                        Inherited = false,
                        Properties = new[]
                        {
                            new MemberPropertyTypeBasic
                            {
                                MemberCanEditProperty = true,
                                MemberCanViewProperty = true,
                                IsSensitiveData = true,
                                Alias = "property1",
                                Description = "this is property 1",
                                Inherited = false,
                                Label = "Property 1",
                                Validation = new PropertyTypeValidation
                                {
                                    Mandatory = false,
                                    Pattern = string.Empty
                                },
                                SortOrder = 0,
                                DataTypeId = 555
                            }
                        }
                    }
                }
            };
        }
    }
    

    However, this code fails in my test project, with the same exception as above, despite being exactly the same as the original. Has anyone have a similar experience and managed to solve it?

    Does anyone know perhaps how I can get more information about the above exception?

    Perhaps anyone has any experience with integration testing with umbraco 8 at all and in particular: setting up a fake database to perform my tests on.

    I am stuck right now, so any help is appreciated.

  • Dennis 16 posts 170 karma points
    1 week ago
    Dennis
    101

    I found the solution!!

    The error was very unspecific, it basically just says: "There's a null reference exception during the creation of one of the dependencies somewhere down the line".

    So I traced all the dependencies and ended up at SettingsForTests.GetDefaultUmbracoSettings(). This step requires the existance of several files: umbracoSettings.config, umbracoSettings.minimal.config and web.config, all inside [MyTestProject]/Configurations/UmbracoSettings.

    I copied these files into my project from the original source, I configured them as Content and Copy if Newer and it worked!! yay! :D

    I hope this helps any of you who have this issue as well!

Please Sign in or register to post replies

Write your reply to:

Draft