Base solution for your next web application
Open Closed

Problem to get entities from DB - MVC and JQuery #3596


User avatar
0
expertit created

Hello,

I'm implementing the "step by step" for MVC Jquery. Everything is fine, page and entities, database migration, inital data feeding.

The problem is when I develop the test for the app service, everything is going well except that no entities are retrieved while in the DB there are records in the table.

In the DbContext

public virtual IDbSet<Subject> Subjects { get; set; }

The entity

    [Table("Subjects")]
    public class Subject : FullAuditedEntity
    {
        public const int MaxNameLength = 32;
        public const int MaxSurnameLength = 32;
        public const int MaxEmailAddressLength = 255;
        [Required]
        [MaxLength(MaxNameLength)]
        public virtual string Name { get; set; }
        [Required]
        [MaxLength(MaxSurnameLength)]
        public virtual string Surname { get; set; }
        [MaxLength(MaxEmailAddressLength)]
        public virtual string EmailAddress { get; set; }
    }

The Interface 

    public interface ISubjectAppService : IApplicationService
    {
        ListResultDto<SubjectsListDto> GetSubjects(GetSubjectsInput input);

    }

The App service

    public class SubjectAppService : EspertisAppServiceBase, ISubjectAppService
    {
        private readonly IRepository<Subject> _subjectRepository;
        public SubjectAppService(IRepository<Subject> subjectRepository)
        {
            _subjectRepository = subjectRepository;
        }

        public ListResultDto<SubjectsListDto> GetSubjects(GetSubjectsInput input)
        {
            var subjects = _subjectRepository
                .GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    p => p.Name.Contains(input.Filter) ||
                         p.Surname.Contains(input.Filter) ||
                         p.EmailAddress.Contains(input.Filter)
                )
                .OrderBy(p => p.Name)
                .ThenBy(p => p.Surname)
                .ToList();

            return new ListResultDto<SubjectsListDto>(subjects.MapTo<List<SubjectsListDto>>());
        }
    }

and the test

    public class SubjectAppService_Tests : AppTestBase
    {
        private readonly ISubjectAppService _subjectAppService;

        public SubjectAppService_Tests()
        {
            _subjectAppService = Resolve<ISubjectAppService>();
        }

        [Fact]
        public void Should_Get_All_Subjects_Without_Any_Filter()
        {
            //Act
            var subjects = _subjectAppService.GetSubjects(new GetSubjectsInput());
            //Assert
            subjects.Items.Count.ShouldBe(2);
        }
    }

In the table Subjects there are two records.

Any idea ?

Best regards,

Albert


1 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    Tests are using fake datas that is not releated with application DB. So you should add test data, first. In tests project, there are example usages (TestDataBuilder.cs) how to add data and retrieve them.