简体   繁体   中英

Xamarin.ios - Using NBuilder to build mocks throws TypeCreationException

I'm using the NBuilder library to build mock http responses, everything works fine in Android, but in iOS each time that I want to build a model class this exception is fired.

"FizzWare.NBuilder.TypeCreationException" and It says that my X model class doesn't have a parametless constructor, which actually has!. For example this model class:

public class Actor
    {
        public Actor() {
        }



        [JsonProperty("authorities")]
        public List<Authority> Authorities { get; set; }
        [JsonProperty("imageDerivatives")]
        public ImageDerivatives ImageDerivatives { get; set; }
        [JsonProperty("profileFileId")]
        public PictureFile ProfilePicture { get; set; }
        [JsonProperty("role")]
        public Role Role { get; set; }
        [JsonProperty("roleId")]
        public int RoleId { get; set; }
        [JsonProperty("status")]
        public bool Status { get; set; }
        [JsonProperty("updatedAt")]
        public DateTime UpdatedAt { get; set; }
        [JsonProperty("username")]
        public string Username { get; set; }
        [JsonProperty("createdAt")]
        public DateTime CreatedAt { get; set; }
        [JsonProperty("departmentId")]
        public int DepartmentId { get; set; }
        [JsonProperty("email")]
        public string Email { get; set; }
        [JsonProperty("firstName")]
        public string FirstName { get; set; }
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("isAppAdmin")]
        public bool IsAppAdmin { get; set; }
        [JsonProperty("lastName")]
        public string LastName { get; set; }
        [JsonProperty("password")]
        public string Password { get; set; }
    }

And this is how I build the mock response:

private Actor GetRandomActor()
        {
            return Builder<Actor>.CreateNew()
                .With(a => a.FirstName = GetRandomFirstName())
                .With(a => a.LastName = GetRandomLastName())
                .With(a => a.ProfilePicture = GetRandomPictureFile())
                .With(a => a.Email = GetRandomEmail())
                .With(a => a.Username = GetRandomUserName())
                .Build();
        }

Try adding the Preserve attribute to your constructor so it does not get removed by the Linker:

[Preserve]
public Actor() {}

(Or add it at the class level [Preserve (AllMembers = true)] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM