Stub a Meteor Method in Cucumber? - meteor

I would like to stub a Meteor method in Cucumber (using Velocity) so that when the Scenario runs and a button is clicked, I do not want it to send an email like it normally would.
My fixtures file in /tests/cucumber/fixtures/fixture.js contains a Meteor.method with the same name as a Meteor.method in the actual app. Of course, this prevents Meteor from running because Method names need to be unique.
I did notice the stubMethod() function here: https://meteor-testing.readme.io/docs/velocity-helpers, but this is marked as a Jasmine-only function.
How can I stub a Meteor method in Cucumber? Thanks.

Firstly you can use xolvio:email-stub to stub Email. If you look through the source code you can see how the stub is built. Note that the docs on GH are a bit out of date, the method names are wrong. You can get the right ones from the code.

Related

An error occurred with Google Calendar API insufficientPermissions

I refer to the document https://developers.google.com/calendar/v3/reference/events/insert to call the insert method, but there is an error insufficientPermissions. I want to know what the general situation is, and I can use gapi.client.calendar.events.list() method to get the result normally.
You are probably using one of the Calendar API Quickstarts which is why calls to events.list works but events.insert doesn't. You need to change the scope to https://www.googleapis.com/auth/calendar because that is required for events.insert.
Also don't forget to delete the previously saved credentials when adding the new scope.

Different parameters in Meteor call stubs

I have a meteor method that's supposed to handle file/image uploads by passing a cdn key, which is just a string.
For latency compensation though, I want to add the actual image blob to LocalMongo, that way I can add an image preview.
This is a problem since I want to just pass a string key to my server method, while I want to pass a file blob to my client method stub. Does Meteor support this? I don't want to pass the image blob to my server (as doing so would serialize the blob/make the call costly).
A solution I'm thinking of is to just define two Meteor methods with different names, the first one being for the client and the other for the server, and just calling them both with the proper parameters. Is this the proper way to do this in Meteor?
EDIT: My solution above doesn't actually work because Meteor realizes there is no method on the server (and nukes the local changes of my client method)
Just a suggestion, you can save the file blob in a Session variable and access in the method when the method stub is called from client, like this,
Meteor.methods({
'yourMethod': function (key) {
if (Meteor.isClient) {
var fileBlob = Session.get('my-file-blob'); //set this variable just before calling this method. And don't forget to remove it when template is destroyed.
} else {
}
}
});
Like I said, I didn't test it but just a suggestion. Hope it helps.

Meteor collection returns no results except in template

I'm new to the Meteor framework, and am having problems accessing data from my collection, outside of a template.
I have a small mongo collection and can retrieve and present its data without problems by using a template. However, when I try to get a cursor or array to use more directly, I get no results returned.
In my script, using find
var dataFind = Fakedata.find();
console.log(dataFind);
console.log(dataFind.count());
gives a cursor object, but a count of zero.
var dataFetch = Fakedata.find().fetch();
console.log(dataFetch);
console.log(dataFetch.length);
gives an empty array, length of zero.
Using the same find() or fetch() from the JS console gives populated objects as I would expect the code above to do. Within a meteor template, everything seems to work fine as well, so the pub/sub seems to be correct.
Any clues as to what I'm doing wrong here?
It looks like your subscriptions aren't ready at the time you try to access your collection data, this is a common gotcha.
When you access your collection data via templates, it is most likely via the use of template helpers which happen to be reactive, so they will rerun when your collections are ready thus displaying the correct data.
When accessing your collections in a non-reactive script though, they will appear empty if the subscription is not yet ready.
You can try using this pattern in your script to execute code only when the subscription is ready :
Meteor.subscribe("mySubscription",function(){
// we are inside the ready callback here so collection date is available
console.log(Fakedata.find().fetch());
});
If you are looking for a more robust approach, try looking at iron:router waitOn mechanism.

List Meteor methods registered with Meteor.methods()?

Wondering if there was a way to get a list of the current Meteor.methods that have been registered.
for example if a post method is registered like so:
Meteor.methods({
post: function() {
//code
}
});
Is there a way to access a list of these methods? Ideally it would be via a method but if it was stored in an accessible variable like Meteor.__methods that would work as well.
I've combed through the documentation and the Meteor global in the browser but did no find anything useful. Any Ideas?
After Digging more on the Server side of meteor, it appears that the methods are stored in an array Meteor.default_server.method_handlers which is accessible on the server but not on the client.
Only way to expose it client side seems to be registering a method server side and then returning a list of the keys.
On the client you can do:
Meteor.connection._methodHandlers
It gives you a dictionary of function names to functions.

How to implement callbacks in Java

I have a class called CommunicationManager which is responsible for communication with server.
It includes methods login() and onLoginResponse(). In case of user login the method login() has to be called and when the server responds the method onLoginResponse() is executed.
What I want to do is to bind actions with user interface. In the GUI class I created an instance of CommunicationManager called mCommunicationManager. From GUI class the login() method is simply called by the line
mCommunicationManager.login();
What I don't know how to do is binding the method from GUI class to onLoginResponse(). For example if the GUI class includes the method notifyUser() which displays the message received from theserver.
I would really appreciate if anyone could show how to bind methods in order to execute the method from GUI class (ex. GUI.notifyUser()) when the instance of the class mCommunicationManager receives the message from the server and the method CommunicationManager.onLoginResponse() is executed.
Thanks!
There's two patterns here I can see you using. One is the publish/subscribe or observer pattern mentioned by Pete. I think this is probably what you want, but seeing as the question mentions binding a method for later execution, I thought I should mention the Command pattern.
The Command pattern is basically a work-around for the fact that java does not treat methods (functions) as first class objects and it's thus impossible to pass them around. Instead, you create an interface that can be passed around and that encapsulates the necessary information about how to call the original method.
So for your example:
interface Command {
public void execute();
}
and you then pass in an instance of this command when you execute the login() function (untested, I always forget how to get anonymous classes right):
final GUI target = this;
command = new Command() {
#Override
public void execute() {
target.notifyUser();
}
};
mCommunicationManager.login(command);
And in the login() function (manager saves reference to command):
public void login() {
command.execute();
}
edit:
I should probably mention that, while this is the general explanation of how it works, in Java there is already some plumbing for this purpose, namely the ActionListener and related classes (actionPerformed() is basically the execute() in Command). These are mostly intended to be used with the AWT and/or Swing classes though, and thus have features specific to that use case.
The idiom used in Java to achieve callback behaviour is Listeners. Construct an interface with methods for the events you want, have a mechanism for registering listener object with the source of the events. When an event occurs, call the corresponding method on each registered listener. This is a common pattern for AWT and Swing events; for a randomly chosen example see FocusListener and the corresponding FocusEvent object.
Note that all the events in Java AWT and Swing inherit ultimately from EventObject, and the convention is to call the listener SomethingListener and the event SomethingEvent. Although you can get away with naming your code whatever you like, it's easier to maintain code which sticks with the conventions of the platform.
As far as I know Java does not support method binding or delegates like C# does.
You may have to implement this via Interfaces (e.g. like Command listener.).
Maybe this website will be helpful:
http://www.javaworld.com/javaworld/javatips/jw-javatip10.html
You can look at the swt-snippets (look at the listeners)
http://www.eclipse.org/swt/snippets/
or you use the runnable class , by overwritting the run method with your 'callback'-code when you create an instance

Resources