A quick way to test units of code with dependencies on other objects is to mock them up. This is not a new concept and the internet is awash with decent mocking frameworks and documentation on how to use them but I thought I'd jot a quick example anyway on how to go about mocking with mono on OS X.
I've built a simple class that has a dependency manually injected into its constructor.
Here is the interface for the dependency:
Implement the interface:
Create a class that depends on the IRepository:
Now when we invoke the above class:
All works as intended, the output is "Test" which is how we implemented the IRepository.
But when testing we want to isolate the MyWorkFactory class without having to rely on the working implementation of the IRepository interface.
So there are a few ways of going about this. We could create a new implementation of IRepository with known outcomes for testing. Or we can mock the dependency up.
I'm using Moq in this example as it's lightweight, works well on mono and is easy to use.
So let's mock/moq up the repository, inject it into the MyWorkFactory class and see what happens:
So as can be seen, the MyWorkFactory object now uses our Mocked repository dependency and outputs "My Mock" so we can test the class in isolation and configure the dependencies to test all avenues.
Showing posts with label OS X. Show all posts
Showing posts with label OS X. Show all posts
Friday, 14 June 2013
Sunday, 10 February 2013
OS X - Super Simple Database Application with C# in 5 steps
There have been a couple of recent developments in the Mono world that have made creating applications on OS X even easier.
One useful tool that has recently appeared on the scene is Nuget. Visual Studio developers have had access to nuget for many years and now and finally it is available to Mono users. It's made very easy to use via Matt Ward's MonoDevelop nuget add in.
This blog post will walk through using Nuget on MonoDevelop on OS X to create a simple database application.
Install the Nuget Addin into MonoDevelop, there are instructions here, I added a new repository via the Add-in repository manager that points to
http://mrward.github.com/monodevelop-nuget-addin-repository/3.0.5/main.mrep
File -> New -> Solution -> Select C# and then Console Project
The model is a simple POCO that will map directly to the database:
Nothing complicated here.
3) Use Nuget to add ServiceStack.Net OrmLite
There are many Object Relational Mappers out there for C#, for example Mono added Microsoft's Entity Framework last year shortly after it became open source. I'll blog about using Entity Framework in the future but for this post I'm going to use an ORM called ServiceStack.Net OrmLite to access a SQLite database.
Goto Project -> Manage Nuget Packages
Add the following package:
In order to use Linq to query the database a few additional references are required, add these by right clicking on the references folder and selecting Edit References...
When run:
One useful tool that has recently appeared on the scene is Nuget. Visual Studio developers have had access to nuget for many years and now and finally it is available to Mono users. It's made very easy to use via Matt Ward's MonoDevelop nuget add in.
This blog post will walk through using Nuget on MonoDevelop on OS X to create a simple database application.
Install the Nuget Addin into MonoDevelop, there are instructions here, I added a new repository via the Add-in repository manager that points to
http://mrward.github.com/monodevelop-nuget-addin-repository/3.0.5/main.mrep
1) Create a new Console Application
File -> New -> Solution -> Select C# and then Console Project
2) Create the model
The model is a simple POCO that will map directly to the database:
Nothing complicated here.
3) Use Nuget to add ServiceStack.Net OrmLite
There are many Object Relational Mappers out there for C#, for example Mono added Microsoft's Entity Framework last year shortly after it became open source. I'll blog about using Entity Framework in the future but for this post I'm going to use an ORM called ServiceStack.Net OrmLite to access a SQLite database.
Goto Project -> Manage Nuget Packages
Add the following package:
- ServiceStack.OrmLite.Sqlite.Mono
Use the search box to locate the package and hit the Add button to add the package to your project:
4) Add a couple of additional references.
In order to use Linq to query the database a few additional references are required, add these by right clicking on the references folder and selecting Edit References...
- System.Core
- System.Data
- System.Data.Linq
5) Use ORMLite to create the database, populate it with data and query it
When run:
The code for this blog post is available for download here.
Monday, 27 August 2012
OS X - Running StyleCop in MonoDevelop on C# code
When developing on Windows I find StyleCop to be a really useful tool for keeping code consistent throughout projects and I sorely miss it when developing C# projects on OS X with MonoDevelop.
I knew about a StyleCop addin for MonoDevelop here but at the time of writing it's not compatible with the latest version of MonoDevelop (3.0.3.5).
Time to get my hands dirty then! I forked the project on GitHub and modified it so that it works with MonoDevelop 3.0.3.5, the repository is here.
Upgrading the project was relatively easy, just tweaking a few version numbers in the addin manifest and reset the references. I also adjusted the StyleCop violations to output as Warnings rather that Errors, this suited the way I like to see the violations.
Getting the addin to run is another matter though - StyleCop uses the registry. Mono has a registry implementation but I found there were security issues involved with non admin processed reading and writing to it so in order to install the addin a little tweak to the registry permissions was required:
Navigate to the registry file:
On my machine it was here:
/Library/Frameworks/Mono.framework/Versions/2.10.9/etc/mono/registry/
Then adjust the security:
sudo chmod 777 last-btime
Here is a screen shot of it running:
Download and build from here:
https://github.com/mattlaver/Stylecop-Monodevelop-Addin
I knew about a StyleCop addin for MonoDevelop here but at the time of writing it's not compatible with the latest version of MonoDevelop (3.0.3.5).
Time to get my hands dirty then! I forked the project on GitHub and modified it so that it works with MonoDevelop 3.0.3.5, the repository is here.
Upgrading the project was relatively easy, just tweaking a few version numbers in the addin manifest and reset the references. I also adjusted the StyleCop violations to output as Warnings rather that Errors, this suited the way I like to see the violations.
Getting the addin to run is another matter though - StyleCop uses the registry. Mono has a registry implementation but I found there were security issues involved with non admin processed reading and writing to it so in order to install the addin a little tweak to the registry permissions was required:
Navigate to the registry file:
On my machine it was here:
/Library/Frameworks/Mono.framework/Versions/2.10.9/etc/mono/registry/
Then adjust the security:
sudo chmod 777 last-btime
Here is a screen shot of it running:
Download and build from here:
https://github.com/mattlaver/Stylecop-Monodevelop-Addin
Sunday, 4 March 2012
MonoMac - Open File Dialogs
Another common feature of applications are file dialogs. This blog post shows how easy it is to use them in our MonoMac applications.
The NSOpenPanel class is used here, the documentation can be seen on the Apple developer library here NSOpenPanel.
Without further ado, let's get stuck in!
1. Create a new MonoMac Project
As per the Hello OS X tutorial, edit the MainWindow.xib in xcode and add a button and an action called _OpenClick and a label called _Label1 with an outlet:
2. A small bit of coding
Add the handler for the button action to MainWindowController.cs:
The NSOpenPanel class is used here, the documentation can be seen on the Apple developer library here NSOpenPanel.
Without further ado, let's get stuck in!
1. Create a new MonoMac Project
As per the Hello OS X tutorial, edit the MainWindow.xib in xcode and add a button and an action called _OpenClick and a label called _Label1 with an outlet:
2. A small bit of coding
Add the handler for the button action to MainWindowController.cs:
partial void _OpenClick(NSObject sender)
{
var openPanel = new NSOpenPanel();
openPanel.ReleasedWhenClosed = true;
openPanel.Prompt = "Select file";
var result = openPanel.RunModal();
if (result == 1)
{
_Label1.StringValue = openPanel.Filename;
}
}
Now Run the application and test the code - pressing the Open button should invoke the Open File Dialog:
Select a random file and observe the Label being updated:
3 Additional notes
The NSOpenPanel can also be used to specify if it is for selecting just files or directories using these properties:
CanChooseDirectories
CanChooseFiles
Also note that the NSSavePanel can be used for saving files.
Monday, 13 February 2012
MonoMac - Getting started with Main Menu Items
Following on from the last post, here is a quick example demonstrating how to add a new items to the application Main Menu. Here we explore using the AppDelegate to pass a NSMenuItem Action to the MainWindowController.
1. Create an Empty project
Create a new MonoMac Project and double click on MainMenu.xib
2. Configure Xcode
From Xcode click the button in the bottom left ("Show Document Outline") and expand the Main Menu:
3. Create the new NSMenuItem place holder
Drag a NSMenuItem from the Object Library and drop it below the "Menu Item - Help" object:
4. Create the Drop down menu
Drag a NSMenu onto the NSMenuItem created in step 3 and set the title of the NSMenu to "My Menu" and the title of the first NSMenuItem to be "MenuTest":
5. Create the NSMenuItem Action
Display the AppDelegate.h file and then hold down [ctrl] and drag the "MenuTest" NSMenuItem onto the code window. Change the Connection to "Action", the Name to _MenuTestClick and then press connect:
6. Add a label to the MainWindow
Select MainWindow.xib (via the navigation menu at the top of xcode) and add a label. Create an outlet by holding down [ctrl] and drag it onto the MainWindowController.h. Call the outlet _Label1:
See the hello-os-x-from-c-five-steps example for more detailed steps on adding label outlets ro rge MainWindowController.h file.
Save Changes and move back to MonoDevelop.
7. One small bit of coding
From Mono Develop, Open MainWindowController.cs and add this method:
1. Create an Empty project
Create a new MonoMac Project and double click on MainMenu.xib
2. Configure Xcode
From Xcode click the button in the bottom left ("Show Document Outline") and expand the Main Menu:
3. Create the new NSMenuItem place holder
Drag a NSMenuItem from the Object Library and drop it below the "Menu Item - Help" object:
Drag a NSMenu onto the NSMenuItem created in step 3 and set the title of the NSMenu to "My Menu" and the title of the first NSMenuItem to be "MenuTest":
5. Create the NSMenuItem Action
Display the AppDelegate.h file and then hold down [ctrl] and drag the "MenuTest" NSMenuItem onto the code window. Change the Connection to "Action", the Name to _MenuTestClick and then press connect:
6. Add a label to the MainWindow
Select MainWindow.xib (via the navigation menu at the top of xcode) and add a label. Create an outlet by holding down [ctrl] and drag it onto the MainWindowController.h. Call the outlet _Label1:
See the hello-os-x-from-c-five-steps example for more detailed steps on adding label outlets ro rge MainWindowController.h file.
Save Changes and move back to MonoDevelop.
7. One small bit of coding
From Mono Develop, Open MainWindowController.cs and add this method:
public void MenuTestClick()
{
_Label1.StringValue = "Menu Clicked!";
}
Open AppDelegate.cs and complete the implementation of the partial method _MenuTestClick (see the designer for the implementation of this partial method):
partial void _MenuTestClick (MonoMac.Foundation.NSObject sender)
{
mainWindowController.MenuTestClick();
}
Now Build and run the application:
Marvellous, a working Main Menu!
Tuesday, 31 January 2012
Hello OS X from C# (Five Steps)
Here is a gentle introduction to developing OS X applications with C#. Hopefully this is useful and gets some more people started with the technology.
To start with, I'm brushing over the technology, the philosophy of design patterns like MVC and how the nuts and bolts work - this is a quick and dirty guide to getting stuck into a simple hello world style application with minimal effort.
Before I begin, there are some prerequisites:
- OS X operating system - I currently use OS X Lion running on a Macbook Pro.
- XCODE 4
- Mono 2.10.8
- MonoDevelop 2.8
XCode, Mono and MonoDevelop are available to download for free and they all installed fine on my mac.
1. Create a Project
Open MonoDevelop and goto File -> New -> Solution:
Select a MonoMac Project, give it an appropriate Name and select Forward and the OK on the project features tab.
Marvellous, we now have a project:
Check it works by right clicking on the project and selecting Run Item:
Exciting eh?
Let's get back to the task in hand. Stop the application from running by going to Run -> Stop.
2. Design the Graphical User Interface (GUI)
Double click on MainWindow.xib and wait for Xcode to open:
In the botton right hand corner is the Object Library (at least on my setup). Drag a NSButton and a NSTextField onto the Window.
Now press the Show the Assistant Editor button to get the hybrid view of the Window and the Code editor:
At the top of the code window there are arrow buttons, use them to cycle through the code files until MainWindowController.h is displayed.
3. Add the Label Outlet
Select the NSTextField on our Window, hold down the [ctrl] button and drag onto the code window. In the Popup window set the Name to be _Label1:
Now press the Connect button and the code should look like this:
4. Add the Button Action
Select the NSButton on our Window, hold down the [ctrl] button and drag to the code window. Change the Connection to Action, the Name to _ButtonClick. The Type box should automatically change to id.
So the Code should now resemble:
When MonoDevelop is refocused it should update it's code files form the changes made in Xcode.
Double click MainWindowController.cs and add the following method:
So in the editor it looks like this:
Now run the application again and press the button:
To start with, I'm brushing over the technology, the philosophy of design patterns like MVC and how the nuts and bolts work - this is a quick and dirty guide to getting stuck into a simple hello world style application with minimal effort.
Before I begin, there are some prerequisites:
- OS X operating system - I currently use OS X Lion running on a Macbook Pro.
- XCODE 4
- Mono 2.10.8
- MonoDevelop 2.8
XCode, Mono and MonoDevelop are available to download for free and they all installed fine on my mac.
1. Create a Project
Open MonoDevelop and goto File -> New -> Solution:
Select a MonoMac Project, give it an appropriate Name and select Forward and the OK on the project features tab.
Marvellous, we now have a project:
Check it works by right clicking on the project and selecting Run Item:
Exciting eh?
Let's get back to the task in hand. Stop the application from running by going to Run -> Stop.
2. Design the Graphical User Interface (GUI)
Double click on MainWindow.xib and wait for Xcode to open:
In the botton right hand corner is the Object Library (at least on my setup). Drag a NSButton and a NSTextField onto the Window.
Now press the Show the Assistant Editor button to get the hybrid view of the Window and the Code editor:
At the top of the code window there are arrow buttons, use them to cycle through the code files until MainWindowController.h is displayed.
3. Add the Label Outlet
Select the NSTextField on our Window, hold down the [ctrl] button and drag onto the code window. In the Popup window set the Name to be _Label1:
Now press the Connect button and the code should look like this:
@interface MainWindowController : NSWindowController {
IBOutlet NSTextField *_Label1;
}
4. Add the Button Action
Select the NSButton on our Window, hold down the [ctrl] button and drag to the code window. Change the Connection to Action, the Name to _ButtonClick. The Type box should automatically change to id.
So the Code should now resemble:
@interface MainWindowController : NSWindowController {
IBOutlet NSTextField *_Label1;
}
- (IBAction)_ButtonClick:(id)sender;
@end
Save (File -> Save) and then go back into MonoDevelop.
5. Wiring up the Button Click Event
When MonoDevelop is refocused it should update it's code files form the changes made in Xcode.
Double click MainWindowController.cs and add the following method:
partial void _ButtonClick(NSObject sender)
{
_Label1.StringValue = "Hello";
}
So in the editor it looks like this:
Now run the application again and press the button:
So as you can see, it's actually quite easy.
Subscribe to:
Posts (Atom)




















