Starting out with unit testable code
So I've read about creating factories, and unit testing driven development and how it actually saves you time during the debug phase of development. I'm using some libraries for 开发者_运维技巧an entity framework (Entity Spaces, since we are not on dot-net 4.0 yet)
Since getting started seems to be the hardest part for me to wrap my brain around, How would I create a factory for an object that is unit testable on a simple method like so?
Public Function castVote(ByVal videoID As Integer, ByVal voterIP As String)
Dim vote As New VoteMachine
vote.VideoID = videoID
vote.VotedOn = Date.Today
vote.VoterAddress = voterIP
vote.Save()
End Function
I'm working on bringing in the class now, I've made my constructor method public and shared like so
Public Shared Function video() As video
Dim thisVideo As New video
Return thisVideo
End Function
However when I call video, its giving me "possible null reference exceptions" errors, and indeed its throwing a null reference. If my constructor is declared public and shared, and is creating a new object, then returning it, why is it giving me null exceptions?
As I understand it, with unit testing (and IOC etc) you'll just about never see a "New {object}" in your code, since doing that wires that code to specifically generated instances of the object in question.
Instead, you might have something like:
Dim vote = MyApp.db.NewVoteMachine
Where MyApp is you're root application, db is your database "layer", and NewVoteMachine is effectively the "factory" method that generates an object that implements VoteMachine.
So, if you needed to mock it, that method could return a mocked VoteMachine object instead of a real one, but the calling code wouldn't know the difference.
This is +really+ simplified though.
精彩评论