Unable to figure out rhino mocks issue - rhino-mocks

In a method in presenter,I expect a method of view to be called.This method is also passed data extracted from a service method(which is not mocked).This service method basically gets data from database and returns List(using LINQ to SQL).Now, when I write this in the test
List<customers> cus = expecteddata;
view.AssertWasCalled(v => v.InitializeCustomersForSelectedCity(cus));
Rhino.Mocks.Exceptions.ExpectationViolationException: ICustomerListView.InitializeCustomersForSelectedCity(System.Collections.Generic.List`1[DAL.Customer]); Expected #1, Actual #0.
The code which I am testing in presenter
public void HandleSelectedCity(int City)
{
selectedCity = City ;
_custometListForm.InitializeCustomersForSelectedCity(_CustomerListService.GetActiveCustomersForSelectedCity(selectedCity));
}
When I ignore arguments, test works fine
What could be the issue?

You assertion creates an expectation based on cus, a variable defined in the unit test. However, when InitializeCustomersForSelectedCity is invoked, it's being invoked with the result of GetActiveCustomersForSelectedCity - a different instance of List<customers>.
Expectations setups basically perform an object.Equals operation on the expected instance and the actual instance. In your case, they are different, and the expectaction is not satisfied.
Either you need to loosen your expectation to accept any List<customers>, or you need to mock GetActiveCustomersForSelectedCity as well so that you can define the returned result from the unit test.

Related

How do I use MethodCall.invoke(someElementMatcher) to create a MethodCall representing a method I subsequently define in an instrumented type?

I am using ByteBuddy to generate a class.
Prior to working with DynamicType.Builder, I was going to store a MethodCall as an instance variable:
private final MethodCall frobCall =
MethodCall.invoke(ElementMatchers.named("frob")); // here I invoke a method I'm going to define as part of the instrumented type
Then later in my generation logic for the instrumented type I define the frob method to do something:
.defineMethod("frob")
.intercept(...etc....) // here I define frob to do something
…and I define the (let's say) baz method to invoke frob:
.defineMethod("baz")
.withParameter(...) // etc.
.intercept(frobCall); // invokes "frob", which I've just defined above
(I am trying to keep this simple and may have mistyped something but I hope you can see the gist of what I'm trying to do.)
When I make() my DynamicType, I receive an error that indicates that the dynamic type does not define frob. This is mystifying to me, because of course I have defined it, as you can see above.
Is there some restriction I am unaware of that prohibits ElementMatchers from identifying instrumented type methods that are defined later? Do I really have to use MethodDescription.Latent here?
It should match all methods of the instrumented type. If this is not happening as expected, please set a breakpoint in MethodCall.MethodLocator.ForElementMatcher to see why the method is not showing up. I assume it is filtered by your method matcher.
I noticed however that it did not include private methods which is now fixed and will be released within Byte Buddy 1.10.18.

Calling OCMStub and OCMReject on the same method

I've been attempting to write some fail fast tests using OCMReject. However I've found that if OCMStub is used in conjunction with OCMReject, this test will pass
id _mockModel = OCMProtocolMock( #protocol( CTPrefModelProtocol));
//It doesn't seem to matter what order these two are in, the test behaves the same
OCMStub([_mockModel getPreferences]);
OCMReject([_mockModel getPreferences]);
[_mockModel getPreferences];
Even though it should clearly fail because I'm calling the function that I've set in the OCMReject method.
I realise I can just stub getPreferences whenever I'm expecting a result from it and remove it from this test, but largely that means if I've set a stub on getPreferences in my setUp method, any test that calls OCMReject([_mockModel getPreferences]) will just be ignored.
Why am I not able to use OCMStub and OCMReject together? Is it because OCMStub alters getPreferences somehow and as a result whenever I call this method, it actually calls some other method instead?
So apparently I can't read. Reading through the OCMock 3 Documentation, under the limitations heading 10.2
Setting up expect after stub on the same method does not work
id mock = OCMStrictClassMock([SomeClass class]);
OCMStub([mock someMethod]).andReturn(#"a string");
OCMExpect([mock someMethod]);
/* run code under test */
OCMVerifyAll(mock); // will complain that someMethod has not been called
The code above first sets up a stub for someMethod and afterwards an
expectation for the same method. Due to the way mock objects are
currently implemented any calls to someMethod are handled by the stub.
This means that even if the method is called the verify fails. It is
possible to avoid this problem by adding andReturn to the expect
statement. You can also set up a stub after the expect.
I suspect this same limitation exists for OCMReject as well. Hopefully this helps equally blind people like myself. A link to the documentation for the lazy.

Call functions from smart contract

could I interact with functions in my deployed contract without using truffle-contract?
I just want to run and play with my functions to check how they work.
I used MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."});
but it doesn't work.
Any idea or suggestions
Thanks
It's hard to know what you mean by "it's doesn't work", but I guess is that you are not seeing any output when running MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}); in the truffle console?
If yes, the reason is because invoking a method to an instance of a contract will give you a Future, and you must handle the result coming back from the call in an asynchronous way. For example, if the function return a value indicating that some computation has happened, you can print the returned value in the console by:
MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}).then(console.log)
If you're writing unit tests (to be executed via truffle test), then you can handle the return value by doing this:
MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}).then(function(returnedValue) {
// do something with the returnedValue, e.g.
// assert.equal(returnedValue, 3, "The returned value must be 3");
});

Equivalent of never(mock) in JMockit

I'm migrating some test cases from JMock to JMockit. It's been a pleasant journey so far but there's one feature from JMock that I'm not able to find in JMockit (version 0.999.17)
I want to check that a mock is never called (any method).
With JMock, all I needed is the following in my Expectations block:
never(mock)
Is it feasible somehow with JMockit?
EDIT:
I might have found a solution but it's not very explicit.
If I put any method of this mock with times =0 in my Expectations block then this mock becomes strict and I believe any method called would trigger an exception.
Try an empty full verification block, it should verify that no invocations occurred on any given mocks:
#Test
public void someTest(#Mocked SomeType mock)
{
// Record expectations on other mocked types...
// Exercise the tested code...
new FullVerifications(mock) {};
}

Can I get the parameters used in an expectation in Rhino Mocks?

I am setting up an expectation for a call to a method that builds and executes a query. I would like to interrogate the properties of the parameter used. Is this possible
using (mocks.Record())
{
Expect.Call(connection.Retrieve(SOMETHING_HERE)).Return(returnedDatay);
}
The bit I am after is the "SOMETHING HERE" bit.
(This is my first time using Rhino mocks)
You can set up constraints on your parameters and on the properties of the parameters. The following code sets up a constraint on a property named MyProperty on your connection object. The mock expects the MyProperty to be 42. Notice, that null is passed as the parameter since it is ignored.
Expect
.Call(connection.Retrieve(null))
.IgnoreArguments()
.Constraints(Property.Value("MyProperty", 42))
.Return(returnedData);
I am writing this from memory so it may not be absolutely correct.
UPDATE:
Rhino Mocks version 3.5 introduces a new extension method GetArgumentsForCallsMadeOn that lets you inspect the parameters passed to the mocked objects:
http://kashfarooq.wordpress.com/2009/01/10/rhino-mocks-and-getargumentsforcallsmadeon/