I tried lots of Google time to find the answer to this and failed (my google searching may also suck :) so in trying to tie a Where and GroupBy together in a Linq query I used the following.
var values = from p in tbl_data
where
p.some_other_value == 'hello world'
select p;
var group_values = values.GroupBy(p => p.a_value).ToList();
I've seen lots of queries that place the groupby in the from and but never with a Where, there may be a nicer way of doing it but the above works for me :)
If you have not already done so, check out Jeff's post on changes to make TestFixture optional. As Jeff notes, Jim and Brad took this line in XUnit and after consultation it was decided for MbUnit to support this. Note, it's optional so your existing tests won't break but if refactoring it out makes sense then it is available for you to do so. If you do have any thought's and feelings on this do let us know.
Jeff announced the release of Gallio alpha 3 yesterday, my time constriants at the moment meant I missed blogging Alpha 2 back in March but that release was a massive release in terms of features and the work that Jeff, Julian, Graham, Mark, Ben and the rest of the team have put in.
I have talked about Gallio before but as a re-cap, Gallio is described best on the website "The Gallio Automation Platform is an open, extensible, and neutral system for .NET that provides a common object model, runtime services and tools (such as test runners) that may be leveraged by any number of test frameworks", What does that mean, well unit test frameworks such as xUnit.net, NUnit, Pex and MSTest can plugin into the framework (and Gallio supports them all) or frameworks like MbUnit v3 use it directly. It's runners then use that framework and Gallio has the largest set of runners of any unit test framework to date.
-
GUI (Icarus)
-
Console (Echo)
-
TD.NET
-
R#
-
VSTS
-
CCNet
-
Powershell
-
MSBuild
-
NAnt
The runners them selfs are very rich, those who remember the ZaneBug framework will remember how rich it's GUI was and that is something we have aimed for in Icarus with great use of layouts and display information. So now you can take your framework of choice and reuse them across the Gallio platform.
Related directly to MbUnit is the change to the parameterized testing, this has had some heavy work done in make things a little simpler and far more flexible. With this kind of testing now added to Xunit.net, NUnit and csUnit there are lots of different ways you can go about it, the following is a reprint from Jeff's announcement.
A test can now be parameterized in any of the following ways:
- Adding a method parameter to a test method.
- Adding a generic method parameter to a test method.
- Adding a constructor parameter to a test fixture.
- Adding a field to a test fixture.
- Adding a writable property to a test fixture.
- Adding a generic type parameter to a test fixture.
Correspondingly data-source attributes can be specified on any of the following code elements:
- Test methods.
- Test method parameters.
- Test method generic parameters.
- Test fixture types.
- Test fixture constructors.
- Test fixture constructor parameters.
- Test fixture fields.
- Test fixture properties.
- Test fixture generic parameters.
So there are many choices, but each choice is quite simple.
Here are a few interesting combinations.
A basic "Row"-test.
CHANGE !! - : The [RowTest] attribute is not needed anymore and has been eliminated.
[TestFixture]
public class Fixture
{
[Test]
[Row(3, 4, 5, true)]
[Row(1, 2, 3, false)]
public void Test(int a, int b, int c, bool expectedResult)
{
Assert.AreEqual(expectedResult, Math.IsPythagoreanTriple(a, b, c));
}
}
CHANGE !! - A generic "Type"-test.
Note: The [TypeFixture] attribute is not needed anymore and has been eliminated.
[TestFixture]
[Row(typeof(List<int>))]
[Row(typeof(LinkedList<int>))]
public class Fixture<T> where T : IList<int>, new()
{
[Test]
public void Test()
{
T list = new T();
list.Add(123);
Assert.AreEqual(1, list.Count);
}
}
CHANGE !! - A combinatorial test.
Note: The [CombinatorialTest] attribute is not needed anymore and has been eliminated.
[Test]
public void Test([Column(0, 1)] int a, [Column("a", "b")] string b, [Column(false, true)] bool c)
{
// Test something with the specified combination.
// Will be executed 8 times total.
}
CHANGE!! - A pairwise test.
Only tests all combinations of pairs of values. Can yield a significant reduction in test cases compared to ordinary combinatorial tests while still obtaining great coverage. See also: www.pairwise.org.
[Test, PairwiseJoin]
public void Test([Column(0, 1)] int a, [Column("a", "b")] string b, [Column(false, true)] bool c)
{
// Test something with the specified combination.
// Will be executed only 4 times instead of 8.
}
NEW ! - A CSV data source.
Just one of many external data sources to be implemented.
[Test]
[CsvData(ResourcePath="CsvDataTest.csv", HasHeader=true)]
public void ImplicitlyScopedResourceWithHeader(decimal price, string item)
{
Log.WriteLine("{0}: {1}", item, price);
}
The data itself should be stored in an embedded resource called "CsvDataTest.csv". We just create a file with that name in the same folder as the test class and then set its Build Action property to Embedded Resource in Visual Studio.
Item, Price
Apples, 1.00
Bananas, 1.50
Cookies, 2.00
Note the changes to TypeFixture and CombinatorialTest, also note Factory is not included, if you use this with combinatorial tests I'd like to hear from you. The Pairwise testing is something new to v3 and I'll cover in more detail in a future post. If you have further questions, things you want to see them also let me know.
Please also note that Jeff sat down with Scott Hansleman, Brad Wilson (XUnit.net), Charlie Poole (NUnit) and Roy Osherove to talk about the past and future of unit test frameworks in .NET at the recent ALT.NET event, Jeff does talk about Gallio and it is well worth a listen. I do hope that in the future the discussion thrown open wider to other folks that are central to the topic, such as Jim Newkirk, Peli de Halleux, Manfred Lange and others.
This question seems to get asked a lot, this recent article on codeproject does a decent job of explanation as well as this article from Infragistics Guidisan Todd Snyder.
If I was sum it up very simply, the different is entry point into the pattern.
In MVC the entry point is the controller, if you look at the Microsoft MVC framework for example the controller class is where you bind your view and everything else (model) together.
In MVP mean while the entry point is the view, webforms lends it's self very well to this pattern as you can bind the aspx and your view and use the normal aspx entry point to get at the rest of your patttern (in fact winforms works well on this pattern as well).
Going a bit further
You can break down your view into two, a view and View-extender. The temptation in webforms land in a passive sense of the pattern is hold all sorts of view centric items on the presenter. If you have lots going on in your presenter for your view and you want to keep your view ultra light and passive then use a view extender as a layer between the presenter and view, remember keep your concerns focused and light.
In Presenter-First (a variant of MVP) you can take your first MVP pattern and extend it's presenter to multiple views that also follow the MVP pattern. If there is a degree of data transform needed between the language of the view and the model then this pattern can be useful.
Crazy thought of the day
I wonder if the Microsoft MVC view engine (or MonoRails for that matter) could be readapted to act as entry point. More likely you could redefine aspx files as your own http handler, writing a class to act as the aspx which then appends the view and obtains actions from it.
For the first time in 7 years of blogging I almost managed no blog posts in a month. I (for fun) graphed the last 12 months of blog posts and sure enough the amount of posts has seen a steady drop. I then graphed all the posts I have made on weblogs.asp.net and since peaking in 2004 I have been on a decline ever since. There are several reasons why, but my now near total flat line needs explanation.
With the work/life balance and my new family growing the balance tips in favour of more time with my family and less time at the computer. As such blogging\writing\opensource and generally all things out side of the day job has gone the way of the pear (i.e I have stopped). I've had a good run, of the 11 years I have been working in this industry all of them have seen me doing things out side of the day job, from blogging, article writing, book authoring and reviewing, to 4 years of user forum support (as a Macromedia Evangelist) and 3 years of open source\MbUnit. I've had time for all these things but things have changed and as much as I love geeking out I won't have Darcey growing up with her dad a stranger. More so, Emma has been for years a 'computer widow' while I was geeking it up, that has to stop.
I came close to closing this blog, I just don't have that much to say anymore. However, Emma convinced me other wise and I'll aim for a couple of posts a month however I may manage none at all.
Lately I've been drafting posts on my daily commute and post them up in batches (just in case you were wondering :).
I recall an article (not sure where I read it) about Ron Jeffries and his ability as an 'alpha architect'. Such people are rare, the design they have mostly in their mind with TDD providing a way of slightly reshaping the design and proving the model (in a pair session that can mean validating each other’s ideas). Another kind of folks is the folks that have a general system picture but design a piece at a time with model shaping and validation occurring as they go. Both are no less a way of designing and developing a system but while the 'alpha architect' has considered overall, system considerations and won't introduce design faults, the other folks need to go carefully to avoid those faults.
The point that TDD is a design process seems to get missed, writing tests before your code is only part of the process and not the sum total. The trouble I have found is that the design process tends to be an organic one, a rhythm that you adopt and you’re not aware of what process you’re following. So in this post I am going to attempt to lay out some of those considerations, not sure I will capture them all and you may have your own, leave us a comment with yours.
SOC
A car is not made from one part but many, with each part such as the engine or wheels made up from other parts. Does the engine depend on the door handle or the windscreen wiper blades? No. Each part, object or concern is separated from the rest but is associated in such a way that you can still achieve your aim without any dependencies on each concern. Relating that back to code, if a car class had an internal Ferrari engine object could we create a Porsche car from that car class? If the car class held a reference to an engine object that the Ferrari object was sub class off could we instead create a Porsche sub class and our Porsche car have the right engine? The practice of SOC is really a good OO practice in that you ensure your classes are so tightly bound to other concerns that you losing all the polymorphic benefits.
TDD helps us here in that we look at all of the concerns of the class and how that class behaves in isolation and with associated concerns integrating with the class. If in writing and then developing the test it fails randomly, your setup and surround is heavy or you are forcing the test into a deep integration test then chances are the class and its concerns are bound up too tight and it's a candidate for further refactoring.
Integration and Isolation
Take your class in isolation, can you isolate it? If you can what are its dependencies, if you’re testing it what kind of dependencies do you need and how deep are they. The more complex it is to take a class in isolation then your likely looking at a smell and some further refactoring is required. TDD forces you into putting a class into isolation and not being 5 levels deep in a system. You’re focused on that class alone and once taken out of context you can really learn if it will hold up.
Jeremy’s most recent post talked about using static and integration styles in your tests, it's a great post and well worth taking your time over. One thing that is worth mentioning about integration is that integration means as light as you can make it. If your using a lot of other actual concrete concerns to achieve isolation then it's a smell, the cohesion between your class and its concerns is too tightly bound. If you can use mocks or doubles in place of the concerns and your class knows no difference, working as it would normally then you’re in a happy place. Sometimes you can't avoid using an actual and sometimes it makes sense but too many and too often would be a smell, finding a balance is half the skill.
Things around
Consider the concerns around the class, what does the class need as inputs, what does it need for its processing and for its outputs. Those considerations would help you decide what you want to mock, double or actual. For example a class adds an entry to a database as a net product, a test (or for that matter another concern making use of the class) does not care how this product occurs; only that it has occurred. It would be a good mock candidate as we are not reusing the db entry further down the test process and as such we only care the method has occurred. If you wanted to emulate some kind of processing or validation, for example loading a collection of data, then a double could be useful. Following the same contract as the actual, the double would be a light weight emulation that the test could use.
Cross Cuts
Cross cutting concerns are (but not limited to) things like logging, security and validation etc. Things that may not be central to the core parts of the class like processing or logic but needed in the general scheme of the operation of the class. If you find your concerns reusing other concerns more and more often than these could be a cross cuts. To aid re-use and isolation you will want to ensure the cross cuts don't affect the SUT when under test. There are several tools are you disposal (such as DI, IoC and AOP) to service the cross cuts.
You may recall ScottGu's post about the new feature in latest drop of the MS MVC framework that allows you to pick your unit test framework of choice. With Mix08 now wrapped up and the framework released I can show you what the MbUnit templates look like. You will need either MbUnit 2.4.2 or MbUnit 3.0 alpha 2, early previews of these are available if you really like the edge, but more stable drops are coming, with 2.4.2 installed here is what you will see.

When created you would see

The test project depends on the name of your project, for example "myproject" would be "myprojecttests" etc. The project is preloaded with your main project and a referance to the MbUnit.Framework dll. For the moment we have a default test for the controller, which looks like.
using MbUnit.Framework;
[TestFixture]
public class HomeControllerTests
{
[Test]
public void About()
{
//
// TODO: Add test logic here
//
}
[Test]
public void Index()
{
//
// TODO: Add test logic here
//
}
}
Notice default tests for your about and index page, these match the default about and index controllers. The template only referances what you need so you don't need to refactor out any unrequired referances.
Note that the MVC team has blogged about how to add and use NUnit templates including NUnit\Rhino templates. To save you some steps we have added the templates to the installers, I did an inital drop of the template but Jeff did all the work to make this happen and we would welcome your feedback.
Back in 2006 I wrote about the co\contra variance generics support in .NET, fast foward to today and with the last few .NET releases not effecting the CLR this is still missing. I have high hopes the next version will bring some changes to the CLR and to ensure that this happens the change report needs some votes, add yours to parent bug here.
I have been watching the development of IronLisp, a Lisp compiler for the DLR for a while now. A little while ago it was released onto codeplex and more recently the project was dropped and reformed as IronScheme (Scheme being a Lisp variant). Great to see another language on the DLE, apart from IronRuby and IronPythoin (and LOLCODE :D) I wonder what other languages are targeting it and if MS have a research\compiler program for it yet?
Todays Flex 3 announcement also carried with it the announcement of an Adobe OSS site. I was looking around the Tamarin site when I noticed the IronMoney project. Being lead by Seo Sanghyeon, an IronPython developer, I at first thought it was an attempt to get ECMAScript Edition 4 running the DLR. However it seems to be an attempt to get IronPython and IronRuby to run on the Tamarin VM. What I can't figure out is why not attempt to run Python\Ruby on Tamarin? I guess in targeting the IL then IronMonkey can execute the assemblies but surely to help better shape the VM in it's support of dynamic languages why not target languages other than ECMAScript?
More Posts
Next page »