Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:36:35+00:00 2026-05-25T16:36:35+00:00

I am start using TDD for the following class using Entity Framework 4.1: public

  • 0

I am start using TDD for the following class using Entity Framework 4.1:

    public class Agent
    {
        // Primary key
        public int ID { get; set; }

        [Required]
        public string Name { get; set; }

        public string Address { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string Phone1 { get; set; }
    }

My assertion will fail:

    /// <summary>
    ///A test for Agent Constructor need have name
    ///</summary>
    [TestMethod()]
    public void AgentConstructorTest()
    {
        Agent target = new Agent();
        Assert.IsNull(target);
    }

When I look at the generated target object, it is created with ID = 0. How could I test that Name is required then?

And if the Name field is required, how could I still create an Agent object? When will the real ID been assigned? To test model itself, do I need create/mockup a DbContext to be able to assigned a ID?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:36:36+00:00Added an answer on May 25, 2026 at 4:36 pm

    Keep in mind that your are just dealing with POCO classes here – there is no magic going on that would allow the construction of the Agent class to fail just because you have put a custom attribute on one of its properties.

    Entity framework is checking for custom attributes during its validation and for data mapping – in this case it will check for the Required attribute and only declare the entity as “valid” if the corresponding string property is not null and also it will map Name to a non-nullable column in the database.

    To mirror that you could write a custom validation routine in your unit test that performs the same checks, i.e. makes sure that all properties that are decorated with the Required attribute indeed have a value, i.e something like this:

    [TestMethod()]
    public void AgentWithNoNameIsInvalid()
    {
        Agent target = new Agent();
    
        Assert.IsFalse(IsValid(target));
    }
    

    This does feel like you are testing EF now though, not your code.

    Since the ID is your primary key it will only be assigned when the entity has been committed to the database. So yes for full testing you will have to mock a unit of work and a context that does this for you as well. There are many pitfalls though and subtle (and not so subtle) differences between IQueryable<T> and IEnumerable<T> that makes this approach very fragile.

    Personally I would recommend you do integration testing with EF based on a separate test database with known content and write your unit tests and expected results based on this test database – this might not be true TDD but I found it is the only way to be sure that I am testing the right thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to start using a key/value store for some side projects (mostly as
I would like to start using TDD in my PHP projects using Symfony 1.4
I'm currently building a class using TDD. The class is responsible for waiting for
I have planed to start using a framework in php development. After going through
I am start using C# System.Threading.Timer class. When the timer is already started and
I start the NServisBus.host.exe file from my wpf application using the following code: System.Diagnostics.Process.Start(NServiceBus.Host.exe);
I'm wanting to start a WPF application using caliburn.micro so I can use TDD
I'm tryng to start using Git on GitHub. Unfortunately I can't manage to get
i recently start using CodeIgniter as PHP MVC FrameWork , before CodeIgniter, i was
I would like to start using DSM, but not sure how to get started.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.