Rhino Mocks AAA Quick Start?

后端 未结 4 796
情深已故
情深已故 2020-12-16 18:48

I\'ve been looking around for some decent information on using Rhino Mocks 3.5+ with the AAA syntax. I find a lot of blogs that have a mix of things from the old and new whi

4条回答
  •  醉话见心
    2020-12-16 18:53

    First make sure you know what you mean for each A in AAA. You may know but I'll include my working definitions for completeness of the answer:

    • Arrange is where I set up inputs, mocks/stubs, the object with the method under test
    • Act is where I call the method under test
    • Assert is where I verify things occurred or didn't according to expectation

    I like to put comments in my test code to remind me to think about each of those things. An example may help clarify: Suppose I have a service layer class which uses two provider layer classes, one from an "old" system and one from a "new" system; I am testing that the method which copies old things to the new system calls the "CreateThing" method one time for each old thing found.

    [Test]
    public void Should_create_new_Thing_for_each_old_Thing()
    {
      // -----
      // arrange
    
      // hardcode results from old system provider
      List oldThings = new List { ... };
    
      // old system provider
      var oldProvider = MockRepository.GenerateStub();
      oldProvider.Stub(m=>m.GetThings()).Return(oldThings);
    
      // new system provider
      var newProvider = MockRepository.GenerateStub();
    
      // service object
      var svc = new MyService(oldProvider, newProvider);
    
      //-----------
      // act
      var result = svc.CopyThings();
    
      //------------
      // assert
      oldThings.ForEach(thing => 
                        newProvider.AssertWasCalled(prov => prov.CreateThing(thing)));
    }
    

提交回复
热议问题