I am new to Unit Testing. The Unit Tests are succeeding in the below. I expect them to fail because Student.getCourse, unidergraduate.getCourse1 and postgraduate.getcourse1 return different values to what is being tested for e.g. "Student Course" <> "Student Course 1".
Imports NUnit.Framework
Imports NMock
Imports StudentCL
Namespace Test
<TestFixture()>
Public Class StudentTest
Private _mocks As MockFactory
Private _StudentMock As Mock(Of Student)
Private _GraduateMock As Mock(Of Graduate)
Private _UndergraduateMock As Mock(Of UnderGraduate)
<SetUp()>
Public Sub SetUp()
_mocks = New MockFactory()
_StudentMock = _mocks.CreateMock(Of Student)()
_GraduateMock = _mocks.CreateMock(Of Graduate)()
_UndergraduateMock = _mocks.CreateMock(Of UnderGraduate)()
End Sub
<Test()>
Public Sub getStudentCourse()
_StudentMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Student Course1")
End Sub
<Test()>
Public Sub getGraduateCourse()
_GraduateMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Graduate Course1")
End Sub
<Test()>
Public Sub getUndergraduateCourse()
_UndergraduateMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Undergraduate Course1")
End Sub
End Class
End Namespace
Public MustInherit Class Student
Public Overridable Function getCourse1() As String
Return "Student course"
End Function
End Class
Public Class Graduate
Inherits Student
Public Overrides Function getCourse1() As String
Return "Graduate course"
End Function
End Class
Public Class UnderGraduate
Inherits Student
Public Overrides Function getCourse1() As String
Return "Undergraduate course"
End Function
End Class
What am I missing?
Your code isn't actually testing anything. It is simply setting up mocks. By themselves, mocks are useless. From what you've written, it appears you want to test the result of the getCourse1 function. An example of one of your tests might look like the following.
<Test()> _
Public Sub getCourse1_WhenCalled_ReturnsExpected()
' Arrange
Dim student As New Graduate()
' Act
Dim course As String = student.getCourse1()
' Assert
Assert.That(course, Iz.EqualTo("Graduate Course1")) ' This will fail as you expected.
End Sub
The test method name follows a convention described in the book The Art of Unit Testing. The test body is arranged in a sequence known as the AAA pattern.
Related
I've got A base class Base and Sorter and Parser classes derived from it .
The same thing with BaseResult with derived SorterResult and ParserResult.
Base has a Result field of BaseResult type, BaseResult has a Log field.
The reason why I've used a Base class, is because both of Parser and Sorter must write a Log.
Here's my code:
Public MustInherit Class Base
Public Result As BaseResult
Event LogChanged()
Protected Sub AddLogLine(ByVal _logString As String)
If Not String.IsNullOrWhiteSpace(_logString) Then Result.Log.Add(_logString)
RaiseEvent LogChanged()
End Sub
End Class
Public Class Sorter
Inherits Base
Public Shadows Result As SorterResult
Sub New()
Result = New SorterResult With {.Log = New List(Of String)}
End Sub
Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
Public Class Parser
Inherits Base
Public Shadows Result As ParserResult
Sub New()
Result = New ParserResult With {.Log = New List(Of String)}
End Sub
Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
Public MustInherit Class BaseResult
Public Log As List(Of String)
End Class
Public Class SorterResult
Inherits BaseResult
'//SorterResult fields
End Class
Public Class ParserResult
Inherits BaseResult
'//ParsedResult fields
End Class
The issue here is that compiler sais(on pic below):
"variable 'Result' conflicts with variable 'Result' in the base class 'Base' and should be declared 'Shadows'." When I used Shadows keyword, warning disappeared, but I get a null reference exception on this line, because Result field is Nothing:
If Not String.IsNullOrWhiteSpace(_logString) Then Result.Log.Add(_logString)
I can't assign a value to a Result variable in Base class constructor, because It must be of type SorterResult in Sorter, and ParserResult in Parser. What is the regular pattern here? Sorry my bad english.
Use generics
Public MustInherit Class Base(Of TResult As BaseResult)
Public Result As TResult
Event LogChanged()
Protected Sub AddLogLine(ByVal _logString As String)
If Not String.IsNullOrEmpty(_logString) Then Result.Log.Add(_logString)
RaiseEvent LogChanged()
End Sub
Public MustOverride Sub Go()
End Class
Public Class Sorter
Inherits Base(Of SorterResult)
Sub New()
Result = New SorterResult With {.Log = New List(Of String)}
End Sub
Public Overrides Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
Public Class Parser
Inherits Base(Of ParserResult)
Sub New()
Result = New ParserResult With {.Log = New List(Of String)}
End Sub
Public Overrides Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
However, this is not a "beautiful" inheritance hierarchy. Inheritance should formulate relations like "a student is a person" where student derives from person. What do sorters and parsers have in common? Are they a Base? Are they loggers? Are they commands (as suggests the Go method)? Is inheritance required here? Wouldn’t it be more appropriate to use aggregation? I would declare a completely independent logger class and inject it into classes. This allows you to be more flexible, as it enables you to inject different types of loggers.
Public MustInherit Class Logger
Public Event LogChanged()
Public MustOverride Sub AddLogLine(ByVal message As String)
Protected Sub OnLogChanged()
RaiseEvent LogChanged()
End Sub
End Class
Public Class TextFileLogger
Inherits Logger
Public Overrides Sub AddLogLine(ByVal message As String)
If Not String.IsNullOrEmpty(message) Then
'TODO: Write message to log file
OnLogChanged()
End If
End Sub
End Class
You can inject it like this:
Public Class SomeConsumerClass
Private _logger As Logger
Sub New(ByVal logger As Logger)
_logger = logger
End Sub
Public Sub DoSomething()
_logger.AddLogLine("Did something!")
End Sub
End Class
Use like this:
Dim obj As New SomeConsumerClass(New TextFileLogger())
obj.DoSomething()
If you have another kind of logger (XmlFileLogger, StringListLogger, DatabaseLogger...) it is now easy to use it without having to change all the classes using it.
Maybe you should even have only one global logger:
Dim globalLogger As New TextFileLogger()
Dim sorter As New Sorter(globalLogger)
Dim parser As New Parser(globalLogger)
Hi StackOverflow Community,
I have a specific situation dealing with Interfaces in VB.Net.
Particularly, I have one function that should be able to run for every child class the interface has. Here is a simple example:
Public Interface Book
Function TurnPage() As Page
Sub OpenBook()
Sub CloseBook()
End Interface
Public Class Reader
Public Sub Reading()
OpenBook()
TurnPage()
CloseBook()
End Sub
End Class
Public Class Magazine
Implements Book
Public Function TurnPage() As Page
Implements Book.TurnPage
' Code implementation for TurnPage
End Function
Public Sub OpenBook()
Implements Book.TurnPage
' Code implementation for OpenBook
End Sub
Public Sub CloseBook()
Implements Book.TurnPage
' Code implementation for CloseBook
End Sub
End Class
Public Class Novel
Implements Book
Public Function TurnPage() As Page
Implements Book.TurnPage
' Code implementation for TurnPage
End Function
Public Sub OpenBook()
Implements Book.TurnPage
' Code implementation for OpenBook
End Sub
Public Sub CloseBook()
Implements Book.TurnPage
' Code implementation for CloseBook
End Sub
End Class
How can I use the Sub in Reader(Reading) for all the child classes(Magazine and Novel) for that particular interface(Book)?
Change you Reader to accept a Book:
Public Class Reader
Public BookToRead as Book
Public Sub New(ABook As Book)
Me.BookToRead = ABook
End Sub
Public Sub Reading()
BookToRead.OpenBook()
BookToRead.TurnPage()
BookToRead.CloseBook()
End Sub
End Class
Then when you need a reader to read a book:
Dim ThreeMusketeers As New Novel()
Dim reader1 as New Reader(ThreeMusketeers) // Novel
reader1.Reading()
Dim NewYorkTimes As New Magazine()
Dim reader2 as New Reader(NewYorkTimes)
reader2.Reading()
Say I have a class with several methods within it. I want to organize the methods into groupings that can be accessed without constructing a new object each time. The purpose is to group the methods of the class into logical buckets
For instance:
Dim myclass as MyCustomClass
myclass.Shipping.Get_List()
myclass.Production.Get_List()
What is the best way to do this? I tried nested classes, but VB.NET won't let me access the methods as shown above.
so this is how i would do what you want
this is not the best design of the world but it would work
I would suggest you to move the actual get_list and other kind of method / property into the specific class while keeping the common one in the parent class, which in this case is test
but then, I have no idea what your code look like so from that point on, it's your choice
Module Module1
Sub Main()
Dim test As New test
test.Production.Get_List()
test.Shipping.Get_List()
End Sub
End Module
Public Class Shipping
Private parent As test
Public Sub New(ByRef parent As test)
Me.parent = parent
End Sub
Public Function Get_List() As List(Of Integer)
Return parent.GetShipping_List
End Function
End Class
Public Class Production
Private parent As test
Public Sub New(ByRef parent As test)
Me.parent = parent
End Sub
Public Function Get_List() As List(Of Integer)
Return parent.GetProduction_List
End Function
End Class
Public Class test
Public Property Production As Production
Public Property Shipping As Shipping
Public Function GetShipping_List() As List(Of Integer)
Return Nothing
End Function
Public Function GetProduction_List() As List(Of Integer)
Return Nothing
End Function
Public Sub New()
Production = New Production(Me)
Shipping = New Shipping(Me)
End Sub
End Class
With caution that you more than likely should re-evaluate your architecture, you could implement your pattern like this:
Public Class MyCustomClass
Private _shippingList As List(Of String)
Private _productionList As List(Of String)
Public Production As ProductionClass
Public Shipping As ShippingClass
Public Sub New()
Production = New ProductionClass(Me)
Shipping = New ShippingClass(Me)
End Sub
Public Class ShippingClass
Private _owner As MyCustomClass
Public Sub New(owner As MyCustomClass)
_owner = owner
End Sub
Public Function Get_List()
Return _owner._productionList
End Function
End Class
Public Class ProductionClass
Private _owner As MyCustomClass
Public Sub New(owner As MyCustomClass)
_owner = owner
End Sub
Public Function Get_List()
Return _owner._productionList
End Function
End Class
End Class
However, if your true intent is simply organizing the methods in a more accessible and logical manner, I would suggest considering:
Public Class MyCustomClass
Public Sub ShippingListGet()
End Sub
Public Sub ShippingListAddTo()
End Sub
Public Sub ShippingThatDO()
End Sub
Public Sub ShippingThisDo()
End Sub
Public Sub ProductionListGet()
End Sub
Public Sub ProductionListAddTo()
End Sub
Public Sub ProductionThisDo()
End Sub
Public Sub ProductionThatDo()
End Sub
End Class
Keep in mind, some people consider that difficult to read. I personally prefer organization along those lines so when the methods are sorted alphabetically they group logically.
I have found the solution I was looking for using interfaces
Public Interface ICompany
Function Company_List() As DataTable
End Interface
Public Class MainClass
Public Company As ICompany = New CompanyClass
Public Sub New()
MyBase.New()
End Sub
Private Class CompanyClass
Public Sub New()
MyBase.New()
End Sub
Public Function Company_List() As DataTable
My code....
End Function
End Class
End Class
I just would like to know how to implement class constructor in this language.
Not sure what you mean with "class constructor" but I'd assume you mean one of the ones below.
Instance constructor:
Public Sub New()
End Sub
Shared constructor:
Shared Sub New()
End Sub
Suppose your class is called MyStudent. Here's how you define your class constructor:
Public Class MyStudent
Public StudentId As Integer
'Here's the class constructor:
Public Sub New(newStudentId As Integer)
StudentId = newStudentId
End Sub
End Class
Here's how you call it:
Dim student As New MyStudent(studentId)
Of course, your class constructor can contain as many or as few arguments as you need--even none, in which case you leave the parentheses empty. You can also have several constructors for the same class, all with different combinations of arguments. These are known as different "signatures" for your class constructor.
If you mean VB 6, that would be Private Sub Class_Initialize().
http://msdn.microsoft.com/en-us/library/55yzhfb2(VS.80).aspx
If you mean VB.NET it is Public Sub New() or Shared Sub New().
A class with a field:
Public Class MyStudent
Public StudentId As Integer
The constructor:
Public Sub New(newStudentId As Integer)
StudentId = newStudentId
End Sub
End Class
I am trying to test that a property has been set but when I write this as a unit test:
moqFeed.VerifySet(Function(m) m.RowAdded = "Row Added")
moq complains that "Expression is not a property setter invocation"
My complete code is
Imports Gallio.Framework
Imports MbUnit.Framework
Imports Moq
<TestFixture()> Public Class GUI_FeedPresenter_Test
Private moqFeed As Moq.Mock(Of IFeedView)
<SetUp()> Sub Setup()
moqFeed = New Mock(Of IFeedView)
End Sub
<Test()> Public Sub New_Presenter()
Dim pres = New FeedPresenter(moqFeed.Object)
moqFeed.VerifySet(Function(m) m.RowAdded = "Row Added")
End Sub
End Class
Public Interface IFeedView
Property RowAdded() As String
End Interface
Public Class FeedPresenter
Private _FeedView As IFeedView
Public Sub New(ByVal feedView As IFeedView)
_FeedView = feedView
_FeedView.RowAdded = "Row Added"
End Sub
End Class
I can't find any examples of moq in VB, I would be grateful for any examples.
See my question Using Moq's VerifySet in VB.NET for the solution to this.