Giter VIP home page Giter VIP logo

bdd-demo's Introduction

Cheat Sheet

Vanilla Machine.Specifications

[Subject("CreditDecisionService")]
class when_the_applicant_has_excellent_credit
{
    Establish context = () =>
    {
        sut = new CreditDecisionService(); 
    };
    Because of = () => response = sut.GetDecision(decisionRequest);

    It should_approve_the_applicant = () => response.Result.Should().Be(DecisionResult.Approved);

    static CreditDecisionService sut;
    static DecisionResponse response;
    static DecisionRequest decisionRequest;
}

Example of a test using Machine.Fakes

class when_the_applicant_has_excellent_credit : WithSubject<CreditDecisionService>
{
    Establish context = () =>
	    The<ICreditReportService>().WhenToldTo(x => x
	    .CheckCreditHistory(Param.IsAny<CreditCardApplication>()))
	    .Return(Builder<CreditReport>.CreateNew()
	    .With(x => x.CreditScore = CreditRating.Excellent.LowerBoundary).Build);

	Because of = () => 
		response = Subject.GetDecision(
			Builder<CreditCardApplication>.CreateNew().Build());

	It should_approve_the_applicant = () => 
		response.Result.Should().Be(DecisionResult.Approved);

	static DecisionResponse response;
}

Specify the subject under test

class when_expired_card_was_detected : WithSubject<CardUpdaterService> 

Verify a method gets called on a mock:

It should_validate_the_card = () => The<IApiWrapper>().WasToldTo(x => x.IsCardValid(card)).OnlyOnce();

Act

Because of = () => Subject.DoSomething();

Set a return value on a fake object

The<IApiWrapper>().WhenToldTo(x => x.IsCardValid(card)).Return(true);

Replace one of the auto-mocked dependencies with a real concrete type.

Configure(x=>x.For<ICardUpdater>().Use<CardUpdater>());

Use NBuilder in your arrangement to create more expressive tests

var address = Builder<Address>.CreateNew()
            .With(x => x.Street = "123 Main St")
            .With(x => x.City = "Dallas")
            .With(x => x.State = "TX").Build();

Use NBuilder to quickly generate a list with some common values

Builder<Address>.CreateListOfSize(10).All()
            .With(x => x.City = "Dallas")
            .With(x => x.State = "TX").Build();

Intercept the arguments passed into a Mock (using MOQ)

string input;
The<IEmailService>().WhenToldTo(x=>x.Send(Param.IsAny<string>)).Callback<string>(y=>input=y);

Get direct access to the underlying mock object and verify a property using MOQ

It should_set_the_burn_device = () => Mock.Get(The<IBurner>()).VerifySet(x => x.BurnDevice = device);

Verify property was set using Machine.Fakes (preferred over the method above)

It should_set_the_burn_device = () => The<IBurner>().BurnDevice.ShouldBe(device);

Force a fake to throw an exception

The<IApiWrapper>().WhenToldTo(x => x.DoSomething())
	.Throw(new Exception("Kaboom!"));

Verify a method was not called

It should_not_lookup_the_applicants_credit_score = () => The<ICreditReportService>()
        .WasNotToldTo(x => x.CheckCreditHistory(Param.IsAny<CreditCardApplication>()));

Verify a method was only called once

It should_lookup_the_applicants_credit_score = () => The<ICreditReportService>()
        .WasToldTo(x => x.CheckCreditHistory(Param.IsAny<CreditCardApplication>())).OnlyOnce();

bdd-demo's People

Contributors

kmcginnes avatar msell avatar

Watchers

 avatar  avatar

Forkers

latish

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.