Saturday, July 23, 2011

RhinoMocks constraint LINQ gotcha

At work the other day I was trying to write some unit tests for a method that used some LINQ statements on a list that I was mocking using RhinoMocks. I put a constraint that a method was called that had a certain count of objects being passed into it via generic IEnumeralbe list. To my suprise, it kept failing saying the constraint had not been made. First thing I did was fire up the debugger and take a look at the list of objects I was passing to the method, and sure enough it was the number I was looking for. I began to start poking around and realized I was passing a list that I had done a LINQ where statement and I never called any method like 'ToList()' or 'ToArray()' to execute the chain of commands I had applied with LINQ. As soon as I called one of those methods the constraint was correct! It seems to be a bug in RhinoMocks that is not actually calling any methods that stop the delayed execution and really make the statement be applied.

Here is a very quick sample that I threw together to show the problem. I used NuGet to get the version of RhinoMocks I used, so I haven't tested this issue with the latest and greatest version per say.



   1:  public class FakeService

   2:      {

   3:          private readonly INumberHelper _numHelper;

   4:          

   5:          public FakeService(INumberHelper numHelper)

   6:          {

   7:              _numHelper = numHelper;

   8:          }

   9:          

  10:          public void PrintAllEventNumbers_NotWorking()

  11:          {

  12:              var evenNumbers = _numHelper.GetAllNumbers()

  13:                  .Where(x => x%2 == 0);

  14:              

  15:              _numHelper.PrintNumbers(evenNumbers);

  16:          }

  17:   

  18:          public void PrintAllEventNumbers_Working()

  19:          {

  20:              var evenNumbers = _numHelper.GetAllNumbers()

  21:                  .Where(x => x % 2 == 0);

  22:   

  23:              _numHelper.PrintNumbers(evenNumbers.ToArray());

  24:          }

  25:      }

  26:   

  27:      public interface INumberHelper

  28:      {

  29:          IEnumerable<int> GetAllNumbers();

  30:          void PrintNumbers(IEnumerable<int> numbersToPrint);

  31:      }



And this is the unit tests to show how it works and doesn't work.



   1:  [Test]

   2:          public void TestDoesNotWork()

   3:          {

   4:              var numHelper = MockRepository.GenerateMock<INumberHelper>();

   5:              var fakeService = new FakeService(numHelper);

   6:   

   7:              numHelper.Stub(x => x.GetAllNumbers())

   8:                  .Return(Enumerable.Range(0, 100));

   9:   

  10:              fakeService.PrintAllEventNumbers_NotWorking();

  11:   

  12:              numHelper.AssertWasCalled(x => x.PrintNumbers(Arg<IEnumerable<int>>.List.Count(Rhino.Mocks.Constraints.Is.Equal(50))));

  13:          }

  14:   

  15:          [Test]

  16:          public void TestDoesWork()

  17:          {

  18:              var numHelper = MockRepository.GenerateMock<INumberHelper>();

  19:              var fakeService = new FakeService(numHelper);

  20:   

  21:              numHelper.Stub(x => x.GetAllNumbers())

  22:                  .Return(Enumerable.Range(0, 100));

  23:   

  24:              fakeService.PrintAllEventNumbers_Working();

  25:   

  26:              numHelper.AssertWasCalled(x => x.PrintNumbers(Arg<IEnumerable<int>>.List.Count(Rhino.Mocks.Constraints.Is.Equal(50))));

  27:          }

Saturday, March 21, 2009

make ASP.Net CheckBoxList mutually exclusive using jQuery

I ran across a stupid problem a few days ago trying to make an ASP.Net CheckBoxList control be mutually exclusive. ASP.Net Ajax has an extender that makes checkbox's mutually exclusive. The problem with it, is that it works with individual checkbox controls, not a CheckBoxList control. I did not want to make them all separate controls because I was binding them to a datasource, and I wanted it to lay out the checkboxes the way it does.

The way the CheckBoxList works makes it impossible to use (maybe I am wrong on this) the ASP.Net Ajax extender control. So my next thought was to write up some javascript to only allow one item to be selected out of all the checkboxs. Since the CheckBoxList controls wraps all the ListItem's in a table (I think it has an inline way to render as well) and that it all has auto generated ID's it makes it hard to write a little custom javascript method to attach to each checkboxes click event.

Then comes jQuery to the rescue! I have been using jQuery recently, and found that its selector helps out ASP.Net development immensly. Because we have such little control of how the HTML renders using the default ASP.Net controls, it makes it hard to apply styles and client side javascript. Below is a small method I modified from someone else to go through and make only one of the checkbox's be able to be in the checked state at a time.

/* This is used to make a list of checkboxes to become
mutually exclusive */
(function($) {
$.fn.makeCheckboxesMutuallyExclusive = function() {
//list of checkboxes
var checkBoxes = this.filter('input:checkbox');
//add a click function to each one to make sure one is checked
checkBoxes.click(function() {
checkedState = $(this).attr('checked');
//loop on all checkboxes and uncheck them
$(checkBoxes).each(function() {
$(this).attr('checked', false);
});
$(this).attr('checked', checkedState);
});
}
})(jQuery);
Take the following snippet of code from an ASPX page, and assume its being bound to a datasource that will populate its list items automatically.

<asp:checkboxlist runat="server" id="chkExample" repeatdirection="Horizontal"></asp:CheckBoxList>


We can then write some javascript to make all the checkboxes in this list mutually exclusive. It looks like this.

$('#<%=chkExample.ClientID %> td input').makeCheckboxesMutuallyExclusive();
This should select all the input controls inside the table that the CheckBoxList wraps its contents in. It then calls the function above with those items to make them mutally exclusive.

Saturday, September 13, 2008

ASP.NET MVC Build Errors in Views

I have been using the ASP.NET MVC since preview 3 and I ran across something very strange today. I had a few errors in lambda expressions in my view for Html.ActionLink(...). I thought that when I built my project these would be caught and the build would fail.

Well, its not happening for me. If the views aren't getting compiled then the whole SCHWEET reason to have strongly typed stuff in there kinda goes out the window!

Am I missing something? Can other people reproduce this problem? Isn't it intended to fail on the build if there are errors in the view?

Thursday, August 28, 2008

VS2008 black background and vmware fusion

So I decided to buy a new macbook (13 inch one) a few weeks ago. I installed vmware fusion and got XP Sp3 up and running real quick. It works very well and is very fast, except one thing that was killing me.

I use VibrantInk theme for VS2008, which has a black background, and when I moved my mouse over the code, I couldn't see my mouse cursor. Well it turned out it was black for some reason. I can't stand the white background, so this was a HUGE issue. But it all ends well.

How to fix:

You have to set the 'Hardware Acceleration' to one under 'Full Acceleration'. This did the trick. It is kind of annoying since the mouse kinda moves around 'laggy' like. I haven't tried the new fusion beta install, but I'll update the post if it works better with that.