How should moq's VerifySet be called in VB.net - vb.net

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.

Related

Access a base class property in inheritance class

I'm using the base class Button in VB.net (VS2017) to create a new class called CDeviceButton. The CDeviceButton then forms as a base for other classes such as CMotorButton, CValveButton.
I want to set the Tag property in the child class CMotorButton but access it in the constructor in CDeviceButton. Doesn't work for me. It turns up being empty.
The Tag is set in the standard property when inserting the CMotorButtom instance into a form.
I've also tried to ensure teh the parent classes' constructors are run by setting mybase.New() as the first action in each constructor but that didn't change anything.
Any ideas for improvements?
Public Class CDeviceButton
Inherits Button
Public MMIControl As String = "MMIC"
Public Sub New()
MMIControl = "MMIC" & Tag
End Sub
End class
Public Class CMotorButton
Inherits CDeviceButton
Sub New()
'Do Something
end Sub
End Class
When you try to concatenate Tag with a string, you are trying to add an object that is probably nothing. I set the Tag property first and used .ToString and it seems to work.
Public Class MyButton
Inherits Button
Public Property MyCustomTag As String
Public Sub New()
'Using an existing Property of Button
Tag = "My Message"
'Using a property you have added to the class
MyCustomTag = "Message from MyCustomTag property : " & Tag.ToString
End Sub
End Class
Public Class MyInheritedButton
Inherits MyButton
Public Sub New()
If CStr(Tag) = "My Message" Then
Debug.Print("Accessed Tag property from MyInheritedButton")
Debug.Print(MyCustomTag)
End If
End Sub
End Class
And then in the Form
Private Sub Test()
Dim aButton As New MyInheritedButton
MessageBox.Show(aButton.Tag.ToString)
MessageBox.Show(aButton.MyCustomTag)
End Sub
Below is my solution I came up with that works. Basically I make sure that all initialization has taken place before reading the Tag property. What I experienced is that the Tag property is empty until the New() in CMotorButton has completed, even though the Tag property has been set when creating the instance of CMotorButton in the Form. TimerInitate has a Tick Time of 500 ms.
Not the most professional solution but works for what I need at the moment.
Another option could be multi threading but that I haven't tried and leave that for future tryouts.
Public Class CDeviceButton
Inherits Button
Public MMIControl As String = "MMIC"
Public Sub New()
TimerInitiate = New Timer(Me)
End Sub
Private Sub TimerInitiate_Tick(sender As Object, e As EventArgs) Handles TimerInitiate.Tick
If Tag <> Nothing Then
TimerInitiate.Stop()
MMIControl = "MMIC" & Tag
End If
End Sub
End class
Public Class CMotorButton
Inherits CDeviceButton
Sub New()
'Do Some stuff
TimerInitiate.Start()
End Sub
Private Sub CMotorButton_Click(sender As Object, e As EventArgs) Handles Me.Click
End Class

Can I use an interface to accomplish this task?

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()

NUnit says the test was successful

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.

Organizing VB.Net Mehods

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

My vb 2003 code can't compile

This is my first post. Please forgive me for asking a basic question as I'm new to programming.
I have following code and it just didn't compile
Module Module1
Public Sub Test
dim a as New TestClass()
dim b as string
b = a.ReturnString()
End Sub
End Module
Public Class TestClass
Public Function ReturnString() as string
Return "Hello World"
End Function
End Class
EDIT: problem solved
Lesson: Need to instantiate class before using it, many thanks to Gens and all of you!
You have 2 End Class statements, remove one.
It looks like you need to put your Test method inside a Module in order for it to Compile
Module Module1
Public Sub Test
dim a as TestClass()
dim b as string
b = a.ReturnString()
End Sub
End Module
Public Class TestClass
Public Function ReturnString() as string
Return "Hello World"
End Function
End Class
EDIT
As was pointed out by Blindy, you had double End Class statements
Try something like this
vbc <filename>.vb
with
Public Class Main
Shared Sub Main
Dim main as New Main
main.Test()
End Sub
Public Sub Test
dim a as New TestClass
dim b as string
b = a.ReturnString()
End Sub
Public Class TestClass
Public Function ReturnString() as string
Return "Hello World"
End Function
End Class
End Class
Your TestClass need to be instantiated before use. To instantiate a class, use new keyword before the class name
dim a as new TestClass()
dim b as string
b = a.ReturnString()