What is the Use of Repeat.Any() in Expect.Call while using Rhino Mocks Test framework - rhino-mocks

When I use Repeat.Any() it doesn't show any error though I don't call GetMood() Method ,but if i don't use n doesn't call GetMood then it shows Excpetion of type ExpectationViolationException.Can somebody tell me what is the use of repeat.any().
MockRepository mocks = new MockRepository();
IAnimal animal = mocks.DynamicMock<IAnimal>();
using (mocks.Record())
{
//Expect.Call(animal.GetMood()).Return("punit");
Expect.Call(animal.GetMood()).Return("Punit").Repeat.Any();
}
//animal.GetMood();
mocks.ReplayAll();
mocks.VerifyAll();

Repeat.Any specifies that GetMood() can be called 0 or more times and that it should return "Punit" if it is called.
The line
Expect.Call(animal.GetMood()).Return("punit");
implies that GetMood must be called exactly once. This is the same as Repat.Once.
You may also use AtLeastOnce, Times, Twice, and Never.

Related

I am trying to use mockk anyConstructed but ending with Missing calls inside every { ... } block

I am trying to return an object whenever a new object of a class is being created.
I have tried using anyConstructed with a spyk or even mockk object of PredictionCheckFunction
every { anyConstructed<PredictionCheckFunction>() } answers { predictionCheckFunction }
It results in the following error on the above line
io.mockk.MockKException: Missing calls inside every { ... } block.
In Mockito I would do something like this
whenNew(PredictionCheckFunction.class).withNoArguments().thenReturn(predictionCheckFunction);
I want to make sure that every creation of PredictionCheckFunction results in an object of predictionCheckFunction
The example in this Question How to mock a new object using mockk allows me to only run a function on a mock object but not return one already created like above Mockito example thenReturn(predictionCheckFunction); -
Example in referred SO Question -
mockkConstructor(Dog::class)
every { anyConstructed<Dog>().bark() }
Any help on how to do this while creation of a new object, is appreciated.

Using let does not save the record

Give the below simple example :
let(:item) { create(:item }
it 'query by scope' do
expect(Item.all.length).to eq 1
end
The test does not pass.
Adding item.save within the it block, the test pass. Using a before(:each) { create(:item) } instead of let make the test pass too.
Sounds obvious, as the point I might be missing, why is let not effectively creating the record in this case?
Please use let! instead. let is lazily evaluated, let! body is called immediately.
All the examples you provided are valid because of let being lazily evaluated. You could also just call item inside it (without save) and it should pass as well.

Spock MissingMethodException

I have something that looks like the similar specification:
def "my spec"(Record record) {
given:
Something something = getSomething()
and:
otherThing = getOtherThing()
doFlow(something, record)
if (record.someType = Types.SOME_SPECIFIC_TYPE) {
doFlow(something, record)
}
}
def doFlow(Something something, Record record) {
when:
//code
then:
//asserts
when:
//code
and:
//mode code
then:
//code
}
However, at runtime, I get: groovy.lang.MissingMethodException: No signature of method doFlow() is applicable for arguments Something, Record values: [given values].
Both "my flow" and "doFlow" are feature methods as they have blocks such as given, when, and then. It's Spock's responsibility to invoke feature methods, and one feature method cannot call another one. If doFlow is meant to be a helper method, it should use explicit assert statements, and shouldn't have any blocks.
PS: Feature methods can't declare method parameters unless they are data-driven (i.e. have a where block).
PPS: A feature method can't just have a given/and block. (You'll get a compile error for this.)

is there way to check if performSelector:withObject:afterDelay: has been registered?

I whould like to know if there is a way to determine if performSelector:withObject:afterDelay: for the given object has been called (registered to be called). (I could use cancelPreviousPerformRequestsWithTarget:selector:object: and re-call performSelector:withObject:afterDelay:, ok but I'm interested to know if there is the alternative).
Thanks
The best thing to do would be to make sure that the selector being called can be called multiple times safely.
For example, use a flag in the target object to track if the method has already been invoked e.g.
-targetSelector: (id) param
{
if (!hasBeenRun) // hasBeenRun is a boolean intance variable
{
hasBeenRun = true;
// other stuff
}
}

How to register component interface in wxwebconnect?

I'm doing an experiment with wxWebConnect test application, incorporating the xpcom tutorial at "http://nerdlife.net/building-a-c-xpcom-component-in-windows/"
I adapt MyComponent class as necessary to compile together with testapp.exe (not as separate dll), and on MyApp::OnInit I have the following lines:
ns_smartptr<nsIComponentRegistrar> comp_reg;
res = NS_GetComponentRegistrar(&comp_reg.p);
if (NS_FAILED(res))
return false;
ns_smartptr<nsIFactory> prompt_factory;
CreateMyComponentFactory(&prompt_factory.p);
nsCID prompt_cid = MYCOMPONENT_CID;
res = comp_reg->RegisterFactory(prompt_cid,
"MyComponent",
"#mozilla.org/mycomp;1",
prompt_factory);
Those lines are copied from GeckoEngine::Init(), using the same mechanism to register PromptService, etc. The code compiles well and testapp.exe is running as expected.
I put javascript test as below :
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const cid = "#mozilla.org/mycomp;1";
obj = Components.classes[cid].createInstance();
alert(typeof obj);
// bind the instance we just created to our interface
alert(Components.interfaces.nsIMyComponent);
obj = obj.QueryInterface(Components.interfaces.nsIMyComponent);
} catch (err) {
alert(err);
return;
}
and get the following exception:
Could not convert JavaScript argument arg 0 [nsISupport.QueryInterface]
The first alert says "object", so the line
Components.classes[cid].createInstance()
is returning the created instance.
The second alert says "undefined", so the interface nsIMyComponent is not recognized by XULRunner.
How to dynamically registering nsIMyComponent interface in wxWebConnect environment ?
Thx
I'm not sure what is happening here. The first thing I would check is that your component is scriptable (I assume it is, since the demo you copy from is). The next thing I would check is whether you can instantiate other, standard XULRunner components and get their interface (try something like "alert('Components.interfaces.nsIFile');" - at least in my version of wxWebConnect this shows an alert box with string "nsIFile".
Also, I think it would be worth checking the Error Console to make sure there are no errors or warnings reported. A magic string to do that (in Javascript) is:
window.open('chrome://global/content/console.xul', '', 'chrome,dialog=no,toolbar,resizable');