SetField() public method

Sets the field.
public SetField ( Sitecore field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context ) : void
field Sitecore The field.
value object The value.
config Glass.Mapper.Sc.Configuration.SitecoreFieldConfiguration The config.
context SitecoreDataMappingContext The context.
return void
        public void SetField_NullPassed_NoExceptionThrown()
        {
            //Assign
            var fieldValue = "";

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldStreamMapper/SetField");
            var field = item.Fields[FieldName];
            string expected = "hello world";

            Stream stream = null;
            var mapper = new SitecoreFieldStreamMapper();

            using (new ItemEditing(item, true))
            {
                field.SetBlobStream(new MemoryStream());
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, stream, null, null);
            }

            //Assert
            var outStream = field.GetBlobStream();
            Assert.IsNull(outStream);

        }
        public void SetField_NullPassed_NoExceptionThrown()
        {
            //Assign
            var templateId = ID.NewID;
            var targetId = ID.NewID;
            var fieldName = "Field";

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    {fieldName, ""}
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),

            })
            {
                var fieldValue = "";

                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];
                string expected = "hello world";

                Stream stream = null;
                var mapper = new SitecoreFieldStreamMapper();

                using (new ItemEditing(item, true))
                {
                    field.SetBlobStream(new MemoryStream());
                }

                //Act
                using (new ItemEditing(item, true))
                {
                    mapper.SetField(field, stream, null, null);
                }

                //Assert
                var outStream = field.GetBlobStream();
                Assert.AreEqual(0,outStream.Length);
            }
        }
        public void SetField_StreamPassed_FieldContainsStream()
        {
            //Assign
            var fieldValue = "";

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldStreamMapper/SetField");
            var field = item.Fields[FieldName];
            string expected = "hello world";

            var stream = new MemoryStream(Encoding.UTF8.GetBytes(expected));
            var mapper = new SitecoreFieldStreamMapper();

            using (new ItemEditing(item, true))
            {
                field.SetBlobStream(new MemoryStream());
            }

            //Act
            using (new ItemEditing(item, true))
            {
              mapper.SetField(field, stream, null, null);
            }

            //Assert
            var reader = new StreamReader(field.GetBlobStream());

            var resultStr = reader.ReadToEnd();
            Assert.AreEqual(expected, resultStr);
        }
        public void SetField_StreamPassed_FieldContainsStream()
        {
            //Assign
            var templateId = ID.NewID;
            var targetId = ID.NewID;
            var fieldName = "Field";

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    {fieldName, ""}
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),

            })
            {
                var fieldValue = "";

                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];
                string expected = "hello world";

                var stream = new MemoryStream(Encoding.UTF8.GetBytes(expected));
                var mapper = new SitecoreFieldStreamMapper();

                using (new ItemEditing(item, true))
                {
                    field.SetBlobStream(new MemoryStream());
                }

                //Act
                using (new ItemEditing(item, true))
                {
                    mapper.SetField(field, stream, null, null);
                }

                //Assert
                var stream1 = field.GetBlobStream();
                stream1.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(stream1);
               
                var resultStr = reader.ReadToEnd();
                Assert.AreEqual(expected, resultStr);
            }
        }