Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: test interfaces unit
I have the constructor below. My question is:
1. Should I extract out interfaces for the objects being created in the constructor and then mock those objects with some mocking framework? 2. How would I test this constructor?
 
Constructor:
protected SomeObjectIveCreated1 member1{ get; set; }
private readonly SomeObjectIveCreated2 member2;
 
public MyConstructor(string name)
        {
            this.member0= name;
            this.member1= new SomeObjectIveCreated1(this as ISomeInterface);
            this.member2= new SomeObjectIveCreated2();
        }  
Posted 19-Nov-12 10:13am
Comments
Sergey Alexandrovich Kryukov at 19-Nov-12 15:36pm
   
Not clear. What interfaces? What is "extract"? Are you talking about unit testing? Unit testing facilities usually provide interfaces containing metadata pointing out what to test, for the automated testing application. What are you trying to do?
--SA
chuckdawit at 28-Nov-12 20:21pm
   
Should I replace the 'SomeObjectIveCreated2' with an interface to adhere to rules of encapsulation?
This is helpful for unit testing but is more for the rules regarding the S.O.L.I.D principles.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

chuckdawit wrote:
Should I replace the 'SomeObjectIveCreated2' with an interface to adhere to rules of encapsulation? This is helpful for unit testing but is more for the rules regarding the S.O.L.I.D principles.
You should understand that interfaces, as opposed to abstract classes and "classic" OOP (before introduction of interfaces) does not really "improve" encapsulation. Encapsulation is more basic OOP feature, not directly related to the heart of OOP, which is related to late binding and polymorphism.
 
Instead, interfaces provides loose coupling between interfaces and implementation. (Please don't be confused here: I used the word "interface" in two different meanings in one phrase — sorry; in first case, "interface" is used in narrow sense of this word, as a kind of type in .NET and other technologies typically using keyword "interface; in second case, the meaning of interface is abstract, in this sense, the content of abstract class and .NET interface are both interfaces.)
 
Besides, in .NET and other technologies without multiple inheritance in the sense C++, interfaces provides the only available multiple inheritance, sometimes called "weak form of multiple inheritance".
 
Typically, you use interfaces when you want some common features outside of the class hierarchy. Moreover, interfaces can be implemented by struct types, which are value types, not reference types. This way, polymorphism based on interfaces, is "more abstract": it hides the difference between reference types and value types: they all are represented in a polymorphic set through some base compile-time type which is the interface managed pointer, as if they all were reference types. And the weak form of multiple inheritance allows the same object instance to participate in different polymorphic sets with different base types. Such kind of loose coupling allows to go beyond the tree structure of inheritance diagram.
 
Interfaces are often used in APIs to free the API users from fitting their implementation types in rigid class hierarchy.
 
Now, you need to identify if you really need the flexibility of interfaces in your application.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100