Skip to content

Instantly share code, notes, and snippets.

@gasparnagy
Last active November 1, 2016 21:28
Show Gist options
  • Save gasparnagy/883303f5549c21252aaa226276888566 to your computer and use it in GitHub Desktop.
Save gasparnagy/883303f5549c21252aaa226276888566 to your computer and use it in GitHub Desktop.
Sample code for the post "Property-based BDD Examples with SpecFlow and FsCheck"
Scenario: Add two numbers
Given I have entered 1 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 3 on the screen
[Binding]
public class Constraints : ConstraintsBase
{
[StepArgumentTransformation("any number")]
public int AnyNumber()
{
return AsParam("any", Arb.Default.Int32());
//could be constrainded: AsParam("any", Gen.Choose(0, 100));
}
[StepArgumentTransformation("the first number")]
public int TheFirstNumber()
{
return AsFormula(actualParams => (int)actualParams.First());
}
}
[TestMethod]
public void FsCheckDemo_Addition_Identity()
{
Func<int, bool> identity =
(a) => Addition.Add(a, 0) == a;
Prop.ForAll(identity).QuickCheckThrowOnFailure();
}
[TestMethod]
public void FsCheckDemo_Addition_Commutativity()
{
Func<int, int, bool> commitative =
(a, b) => Addition.Add(a, b) == Addition.Add(b, a);
Prop.ForAll(commitative).QuickCheckThrowOnFailure();
}
@propertyBased
Scenario: Identity property
Given I have entered any number into the calculator
And I have entered 0 into the calculator
When I press add
Then the result should be the first number on the screen
Scenario: Should be able to choose color
Given the user is logged in
And selected a *product with color variations*
When the user adds the product to the basket
Then it should be able to choose the color
Scenario: Restricted pages require login
Given the user has not logged in yet
When the user tries to access a *restricted page*
Then the user should be redirected to the login page
Scenario: Do not let the user type while driving
Given the vehicle is driving with speed *greater than 5 km/h*
When the driver attempts to type in an address
Then a warning should be displayed
public static int Add(int op1, int op2)
{
// "after 38, every year counts twice"
return op1 >= 38 ? op1 * 2 + op2 : op1 + op2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment