Skip to content

Commit 4b86ca5

Browse files
committed
More old blogs + comments
1 parent 360cf73 commit 4b86ca5

17 files changed

+1097
-71
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
id: 1yeuw819q
2+
date: 2013-05-06T16:03:00.2306994Z
3+
name: Royce Roy
4+
email:
5+
avatar: /assets/images/comments/avatar-unknown.gif
6+
url:
7+
message: |
8+
This is awesome!! really helpful for me. Thanks for sharing with us. Following links also helped me to complete my task.
9+
10+
[https://www.mindstick.com/Articles/1c6b6e52-2e04-4462-9d85-1d7706077ef0/?Routers%20and%20Controller%20in%20ASP%20NET%20MVC%203](https://www.mindstick.com/Articles/1c6b6e52-2e04-4462-9d85-1d7706077ef0/?Routers%20and%20Controller%20in%20ASP%20NET%20MVC%203)
11+
12+
[https://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs](https://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: 1hwiuq9q
2+
date: 2013-05-06T14:09:00.2306994Z
3+
name: KR
4+
email:
5+
avatar: /assets/images/comments/avatar-unknown.gif
6+
url:
7+
message: How did you get WatiN to recognize the extension method as a member of the class?
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: jw91owlq
2+
date: 2013-05-06T18:11:00.2306994Z
3+
name: Steve
4+
email:
5+
avatar: https://www.gravatar.com/avatar.php?gravatar_id=bc7436a66cd16a47a5a382d2c6e7c6a3&size=50
6+
url:
7+
message: I'm not sure what you mean? The extension methods work the same as any extension methods - so long as you've got a using for the namespace where you've defined them they should show up as callable members.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: 1hwiuq9q
2+
date: 2017-07-07T16:08:00.2306994Z
3+
name: Javier Callico
4+
email:
5+
avatar: https://www.gravatar.com/avatar.php?gravatar_id=3826cdf88c34c6f6c83700a9db1268d2&size=50
6+
url:
7+
message: Thanks a lot for sharing.... your extension did the trick for me.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
layout: post
3+
title: A Friendlier NUnit PredicateConstraint
4+
excerpt: I've taken to using NUnit's constraint model for my unit tests, as I find it a great way to write readable tests which state exactly what they prove. NUnit has lots of built-in Constraints, but its PredicateConstraint doesn't return very helpful error messages; here's a friendlier version, which does.
5+
tags: [C#, Automated Testing]
6+
---
7+
8+
After recently reading [a book](https://www.amazon.co.uk/Growing-Object-Oriented-Software-Guided-Signature/dp/0321503627)
9+
on effective unit testing, I've taken to using the
10+
[NUnit constraint model](https://www.nunit.org/index.php?p=constraintModel&r=2.4) for my unit tests.
11+
I find it a great way to write readable tests which state exactly what they prove; for example, it's
12+
pretty clear what this test shows, just from reading the code as a sentence:
13+
14+
```csharp
15+
Assert.That(AnAccount(WithNoOrders), HasAnOutstandingBalanceOf(Zero));
16+
```
17+
18+
In NUnit's `Constraint` model the first argument to `Assert.That` returns an object to test, and
19+
the second returns an NUnit `Constraint` which determines if the test object is in an expected state.
20+
In this example the `AnAccount()` method would return an `Account` object, and the
21+
`HasAnOutstandingBalanceOf()` method would return an NUnit `Constraint` which tests the
22+
`Account.OutstandingBalance` property against an expected value.
23+
24+
NUnit has simple `Constraint`s for checking if an object under test is null, an empty collection, etc,
25+
and for more complex tests it has a
26+
[`PredicateConstraint`](https://code.google.com/p/nunit4netce-and-silverlight/source/browse/trunk/NUnit-2.5.3.9345/nunit.framework/Constraints/PredicateConstraint.cs?spec=svn2&r=2),
27+
which takes a `Predicate` to run. The `HasAnOutstandingBalanceOf` method in our example could use
28+
a `PredicateConstraint` to run its test like this:
29+
30+
```csharp
31+
private static Constraint HasAnOutstandingBalanceOf(
32+
int expectedOutstandingBalance)
33+
{
34+
return new PredicateConstraint<Account>(
35+
a => a.OutstandingBalance == expectedOutstandingBalance);
36+
}
37+
```
38+
39+
When the test runs, the test runner will call the supplied `Predicate` to check if the test has
40+
passed. The thing is, if the test fails, the NUnit output will say something like this:
41+
42+
```csharp
43+
Expected: <a value matching lambda>
44+
Was: 1.00
45+
```
46+
47+
Now, **'&lt;a value matching lambda&gt;'** isn't a very helpful message when diagnosing what the
48+
test was expecting. To get more helpful messages, I've written a `FriendlyPredicateConstraint`;
49+
it takes a predicate just like the `PredicateConstraint`, but also takes the expected value and
50+
a Func to retrieve the actual value, so these can be used in the test results if something goes wrong.
51+
52+
Here's the code:
53+
54+
```csharp
55+
using System;
56+
using NUnit.Framework.Constraints;
57+
58+
public class FriendlyPredicateConstraint<T> : PredicateConstraint<T>
59+
{
60+
private readonly string _expectedValue;
61+
private readonly Func<T, object> _actualValueGetter;
62+
63+
public FriendlyPredicateConstraint(
64+
Predicate<T> matchingPredicate,
65+
object expectedValue,
66+
Func<T, object> actualValueGetter)
67+
: base(matchingPredicate)
68+
{
69+
_expectedValue = expectedValue.ToString();
70+
_actualValueGetter = actualValueGetter;
71+
}
72+
73+
public override bool Matches(object actual)
74+
{
75+
// This method is called to find out if the object returned
76+
// by the test satisfies the Constraint:
77+
bool matches = base.Matches(actual);
78+
79+
if (!matches && (_actualValueGetter != null) && (actual is T))
80+
{
81+
// The test failed, so retrieve the actual value from the
82+
// object under test. 'this.actual' is a property in the
83+
// base
84+
// class which supplies the test output with the actual
85+
// value:
86+
this.actual = _actualValueGetter.Invoke((T)actual);
87+
}
88+
89+
return matches;
90+
}
91+
92+
public override void WriteDescriptionTo(MessageWriter writer)
93+
{
94+
// If the test fails then this method is called to print the
95+
// expected value to the test output:
96+
writer.WriteExpectedValue(_expectedValue);
97+
}
98+
}
99+
```
100+
101+
So now HasAnOutstandingBalanceOf can be rewritten like this:
102+
103+
```csharp
104+
private static Constraint HasAnOutstandingBalanceOf(
105+
int expectedOutstandingBalance)
106+
{
107+
return new FriendlyPredicateConstraint<Account>(
108+
a => a.OutstandingBalance == expectedOutstandingBalance,
109+
// The expected value:
110+
"OutstandingBalance = " + expectedOutstandingBalance,
111+
// The actual value:
112+
a => "OutstandingBalance = " + a.OutstandingBalance);
113+
}
114+
```
115+
116+
...and if the test fails, the output will say this:
117+
118+
```csharp
119+
Expected: OutstandingBalance = 0.00
120+
Was: OutstandingBalance = 1.00
121+
```
122+
123+
Which is much more helpful :)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
layout: post
3+
title: Handy Generic JQuery Functions
4+
excerpt: I was a bit of a late-comer to the JQuery party, but now I've been using it for a while it's given me a host of options for adding extra flair to the client side of my applications. Here's a few generic JQuery functions I've written which can be used to add some neat little features to a page. Just call any of them from a document ready function.
5+
tags: [C&#35;, JavaScript, JQuery]
6+
---
7+
8+
I was a bit of a late-comer to the JQuery party, but now I've been using it for a while it's given
9+
me a host of options for adding extra flair to the client side of my applications. Here's a few
10+
generic JQuery functions I've written which can be used to add some neat little features to a page.
11+
Just call any of them from a document ready function.
12+
13+
## Apply JQuery Themeroller Styles to all Page Buttons
14+
15+
The [JQuery Themeroller](https://jqueryui.com/themeroller) is a great tool for creating a theme for
16+
a site based on colours and styles for particular page elements. The [JQuery.UI](https://jqueryui.com)
17+
library then provides a set of functions which allow you to apply styles to page elements. This
18+
function applies a JQuery Themeroller style to all the buttons on a page - as well as any elements
19+
which have a `button` class applied to them - and then makes the mouse pointer turn into a cursor
20+
when you mouse over them:
21+
22+
```js
23+
function addCursorPointerToButtons() {
24+
$("button, input[type='submit'], input[type='button'], .button")
25+
.button().css("cursor", "pointer");
26+
}
27+
```
28+
29+
## Automatically Remove the Default Value from a Select Box
30+
31+
Required drop-down select boxes often have a default option which reads 'Please select...' (or something
32+
like that), but once someone has selected a value, there's no need to retain that. This function removes
33+
the default option from any select boxes on the page which have a `data-val-remove-default` attribute
34+
once one of the non-default options has been chosen:
35+
36+
```js
37+
function removeDefaultSelectOptionOnSelect() {
38+
$("select[data-val-remove-default='']").change(function () {
39+
var sel = $(this);
40+
if (sel.val() != "") {
41+
sel.children("option[value='']:first").remove();
42+
}
43+
});
44+
}
45+
```
46+
47+
## Automatically add a Required Label and Stars to a Form
48+
49+
It's standard to have a little \* next to required form field elements. This function adds the text
50+
**\* Required** to the top of the first form on the page, and adds \*s to any element within the form with
51+
the class `editor-label` and a `data-val-required` attribute:
52+
53+
```js
54+
function addRequiredFieldLabels() {
55+
var elements = $(".editor-label[data-val-required='']");
56+
if (!elements.length) { return; }
57+
58+
var requiredString =
59+
"<div class='editor-required-key'>* Required</div>";
60+
var prependString =
61+
"<span class='editor-required-label'> * </span>";
62+
63+
var firstFormOnThePage = $("form:first");
64+
65+
if (!firstFormOnThePage.children('div.editor-required-key').length) {
66+
firstFormOnThePage.prepend(requiredString);
67+
}
68+
69+
elements.each(function (index, value) {
70+
var formElement = $(this);
71+
if (!formElement.children('span.editor-required-label').length) {
72+
formElement.prepend(prependString);
73+
}
74+
});
75+
}
76+
```
77+
78+
I hope those come in handy :)

0 commit comments

Comments
 (0)