locked
Unit Test Dao Generic Method RRS feed

  • Question

  • Hi!

    I have a generic method to get a record by its id.

    public T Get(TId id)
        {
          using (var db = new CustomerServiceModelContainer())
          {
            T returnItem = db.CreateObjectSet<T>().Where(string.Format("it.{0} = @id", GetPrimaryKeyName()), 
              new ObjectParameter[] { 
                new ObjectParameter("id", id) 
              })
              .FirstOrDefault<T>();
            return returnItem;
          }
        }
    

    I'm starting with Unit Testing and I'dlike to know how can I uni test this method, because I'm not really sure about how valuable it is.

    IMO, Its nice to test this method, because before I had new ObjectParameter("id", id.ToString()), which gave me a nice Exception. So I'm looking to avoid or to be warned about this kind of issue.

    Thanks for helping,

    Danilo

    Wednesday, February 2, 2011 3:32 PM

All replies

  • I would test it like that :

    
    [TestMethod]
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void GetNTest()
    {
    Object obj = new Object();
    obj.Get(-1); }
    [TestMethod()] public void GetPTest() { //Add record to database from object called for example customerToD . Object obj = new Object(); var customer = obj.Get(id); assert.areequal(customer.name,customerToD.name); }

    You have only one param and only one path (there is no if,case etc) o input can be valid or not. output can be valid or not. So for invalid output is GetNTest and for valid there is GetPTest. Output is generate by returnItem so return object is valid or not.
    Thursday, February 3, 2011 1:23 PM