Rhino.Mocks basics - I just threw this together real quick; personal reference material
http://house9-code-samples.blogspot.com/2008/02/rhinomocks-basics.html
The official Rhino Mocks Documentation is very good and available here
http://www.ayende.com/Wiki/(S(33lpuuuz4itdtnjwlncidy55))/Default.aspx?Page=Rhino+Mocks+Documentation
The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.
Saturday, February 16, 2008
Rhino.Mocks - the basics
Started using Rhino.Mocks recently (Awesome!!!) - just wanted to list a few of the basics; the official Rhino Mocks Documentation is very good and available here. These examples are a bit rough, they have not been compiled; there may be typos.
using
The mock repository
Mock a call to the db
Ignore any arguments
mock a method that returns void
Handle multiple calls to the same method
Partial Mock
using
using Rhino.Mocks;
The mock repository
// use the mock repository to create the mocked objects MockRepository mocks = new Rhino.Mocks.MockRepository(); // example System.Object someObject = mocks.CreateMock(); // set up the behaviour you want from calls to your mocked object Rhino.Mocks.Expect.Call(someObject.Equals(someParameter)).Return(false); // then don't forget mocks.ReplayAll(); // now do stuff that calls your mocked object Equals method ...
Mock a call to the db
// create our mocked repository object - IProductRepository, the real one calls the db IProductRepository repository = mocks.CreateMock(); // tell rhino.mocks when we call the method GetProduct with the argument 33 to return null Rhino.Mocks.Expect.Call(repository.GetProduct(33)).Return(null); // get the mocking ready mocks.ReplayAll(); // pass our mock to our product service layer ProductService service = new ProductService(repository); // service.GetProduct makes a call to the mocked repository.GetProduct which will return null IProduct product = service.GetProduct(33); // should be null Assert.IsNull(product);
Ignore any arguments
// any call to GetProduct will return null no matter the value of the arg Rhino.Mocks.Expect.Call(repository.GetProduct(null)).Return(null).IgnoreArguments();
mock a method that returns void
IProduct product = new Product("003092", "Sierra Nevada", 6.49);
// this void call will handle our product 003092
Rhino.Mocks.Expect.Call(delegate { repository.Save(product); });
// this void call will handle any argument
Rhino.Mocks.Expect.Call(delegate { repository.Save(null); }).IgnoreArguments();
Handle multiple calls to the same method
IProduct product = new Product("003092", "Sierra Nevada", 6.49);
// note the .Repeat.Any()
Rhino.Mocks.Expect.Call(repository.GetProduct("003092")).Return(product).Repeat.Any();
// send the mock to our product service object
ProductService service = new ProductService(repository);
// find by sku calls GetProduct on the mocked Product repository
IProduct beer = service.FindBySku("003092");
// find decent beer calls GetProduct on the mocked Product repository also
IProduct decentBeer = service.FindDecentBeer();
Partial Mock
// create a partial mock with constructor args (our product repository again) IProductService service = mocks.PartialMock(repository); // our test case data IUser loggedOnUser = new User("John", "Doe", CustomerType.Gold); // mock product service calls to GetLoggedOnUser Rhino.Mocks.Expect.Call(service.GetLoggedOnUser()).Return(loggedOnUser); // get the mocking ready mocks.ReplayAll(); // now GetDiscount is not a mocked method but it does call the mocked GetLoggedOnUser method decimal discount = service.GetDiscount(); // assert the result Assert.AreSame(44.4M, discount, "Gold customer discount not correct?"); // NOTE: this will not work (compile) unless the mocked method GetLoggedOnUser is public
Labels:
code,
csharp,
rhino.mocks,
unit test
SyntaxHighlighter and Google Blogger
Just started using the SyntaxHighlighter on my code samples blog - here is some quick notes on what I had to do
- Download the SyntaxHighlighter javascript and css files from http://code.google.com/p/syntaxhighlighter/
- Upload those to my googlepage website, if you don't have one go to https://www.google.com/accounts/ManageAccount and click on the 'Page Creator' link
- Edit the blog template -> Layout -> Edit Html
- Add the references to the css and javascript files in the head section
- Add the initialization javascript calls right before the /body tag at the end of the html template
- see http://morten.lyhr.dk/2007/12/how-to-get-syntax-highlighting-in.html
- then use pre tags like this around your code pre class="c-sharp" name="code"
- see http://code.google.com/p/syntaxhighlighter/wiki/Usage
One issue I ran into was that the google blogger automatically adds br tags where newlines appear in any content - makes the code pretty much unreadable, but the fix is well documented and easy to implement - http://code.google.com/p/syntaxhighlighter/wiki/BloggerMode
Labels:
code
Sunday, January 6, 2008
mssql reporting services - logs
a while back I had to set up reporting services; most of the errors do not get logged to the event log, instead they are logged to a file:
{drive}\Program Files\Microsoft SQL Server\{instance}\Reporting Services\LogFiles
{drive}\Program Files\Microsoft SQL Server\{instance}\Reporting Services\LogFiles
Labels:
coding
Saturday, December 15, 2007
Personal Web Page Revisited
Ok, it has been 5 years since I have updated my personal webpage, once again it is just a 1 page web site; lean and mean ~:P
It might load a bit slow; I am using some of the libraries from Ext JS - a few hundred K in css, images and js to load in, should cache in the browser after the first hit, their UI widgets are just too cool.
anyhow here it is - http://www.jessehouse.com/
It might load a bit slow; I am using some of the libraries from Ext JS - a few hundred K in css, images and js to load in, should cache in the browser after the first hit, their UI widgets are just too cool.
anyhow here it is - http://www.jessehouse.com/
Labels:
misc
Random Coding Links - #1
Random Coding Links - #1
wanted to keep track of these somewhere
wanted to keep track of these somewhere
Labels:
coding
Subscribe to:
Posts (Atom)