This is a migrated thread and some comments may be shown as answers.

What is Arranging?

3 Answers 69 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 04 Jan 2011, 04:10 AM
I'm still trying to figure out how this all works (on the fence on Mocking right now)

Can someone explain what's going on here?
// Arrange
var filename = this.GetType().Assembly.ManifestModule.FullyQualifiedName;
var fi = new FileInfo(filename);
 
bool called = false;
 
Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
 
// Act
fi.Delete();
 
// Assert
Assert.IsTrue(called);

In plain english, what is this line doing
Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
Is it saying that when .Delete is called on the fi object, dont do the default, instead just return true or something?

What is the () => lambda syntax mean?

3 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 05 Jan 2011, 10:58 AM
Hi Steve,

Thank you for bringing up the questions. Regarding your first point on what the following line refers to:

Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);

In this line you are actually setting up / arranging  such that when  fi.Delete is called it will rather go through / evaluate your custom expectation than the original (if not mocked) or default if invoked on a mocked instance (created using Mock.Create<T>()). Here in this particular case, I am just setting a variable with some value and asserting it in later stage.

Next, the lambda syntax ()=> defines an anonymous delegate. Mock.Arrange parses the expression within it (not executing) to extract the member for settting up with your custom expectation. Additionally, any call within Mock.Arrange should follow it.

Please do write back for any further confusion.

Kind Regards,
Ricky
the Telerik team

Browse the videos here>> to help you get started with JustMock
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 06 Jan 2011, 02:50 PM
Hey Ricky thanks for the reply :)

Hmmm, okay I think I'm just getting confused by the samples then...

(like this)
var foo = Mock.Create<Foo>();
Mock.Arrange(() => foo.Echo(1)).Returns(10);
Assert.Equal(foo.Echo(1), 10);

Doesn't seem like I'm testing anything but JustMock...making sure the arrange statement returns 10 if I pass in 1.

Can you quickly explain Mock.Assert in more detail?  The way it reads, the Arrange doesn't call the actual method, but fakes it returning the value you specify.  So why would a Mock.Assert on that method (checking if it Occurs Once) check anything...is it to make sure the Mocked object doesn't do more than it's supposed to, or is it to check the original object to make sure during the test doesn't internally use it a bunch.

See this as an example from the docs
[TestMethod]
public void ShouldAssertOccursAtLeast()
{
    var foo = Mock.Create<IFoo>();
    Mock.Arrange(() => foo.Submit()).OccursAtLeast(2);
     
    foo.Submit();
    foo.Submit();
    foo.Submit();
                 
    Mock.Assert(foo);
}
Ok, so I dont know why this would even be a test.  I make a fake object, make sure Submit is only called twice, then I call the fake objects submit method 3 times.  I mean I KNOW why it's like this, to show a failing test, but why would I be asserting something like that in a test if I'm manually calling the method.  If I'm writing the code to do that (call it) then it could potentially be different when used in the actual code (not the test)


 (fyi the docs have a bug, same items listed twice w/typo)
I very much think I want to start using this, I'm just trying to understand the point at which I can (or, how to use it in our tests)

No rush on an answer :)

Thanks,
Steve
0
Ricky
Telerik team
answered on 11 Jan 2011, 01:43 PM
Hi Steve,

Thanks again for making the post.

The reason for having the basic tests is to demonstrate the various capabilities of JustMock. Though, that is one of the main reasons to include the Fixture tests, similar to samples in other tools like for example Moq. But if you consider the following test method (similar to one you have shown):

public void ShouldThrowForUnUsedSetup()
{
   var foo = Mock.Create<IFoo>();
   Mock.Arrange(() => foo.Echo(1)).Returns(10);
   Assert.Throws<MockAssertionException>(() => Mock.Assert(() => foo.Echo(1));
  
}

Here In addition to this showing that JustMock should throw MockAssertionException for setups that is never invoked, also gives developer a clue on how the tool behaves thus makes it quite useful in learning functionalities through a more TDD driven way.

I would also recommend you to have a look at the quick-start examples in the online documentation:
http://www.telerik.com/help/justmock/getting-started-quick-start.html
 
Next, in regard to your question about what is Mock.Assert.  Basically, Mock.Assert works in a way that it ensures if the expectations are met as defined. Now, let's again cosider  the example of occurrence from the previous post:

[TestMethod]
public void ShouldAssertOccursAtLeast()
{
    var foo = Mock.Create<IFoo>();
    Mock.Arrange(() => foo.Submit()).OccursAtLeast(2);
       
    foo.Submit();
    foo.Submit();
    foo.Submit();
                   
    Mock.Assert(foo);
}

Here in arrange it tells the tool that foo.Submit() must occur at least twice. Therefore, the above assertion will pass however if it were a negative test then (like foo.Submit() was called less than expected)  we would have written the assertion in this way :

Assert.Throws<MockAssertionException>(() => Mock.Assert(foo));

In a nutshell, Mock.Assert gives you a way to ensure that your target system (SUT) executed the mocked call as expected.

Moving forward, you can assert in two ways.

  • You can directly assert the expected member via lambda in Mock.Assert
  • You can assert for all the expectations like above. This will only consider the must (marked via MustBeCalled) or to be occurred a certain number of times setups.

Finally, sorry for the typo in the documentation, we will fix that asap. Should you have any more issues please don’t hesitate to contact us.

Kind Regards,
Ricky
the Telerik team

Browse the videos here>> to help you get started with JustMock
Tags
General Discussions
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Ricky
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Share this question
or