-
Notifications
You must be signed in to change notification settings - Fork 53
Description
Hi Martin! Long time no write, hope you are fine :-)
I'm testing a service that calls a method on an application (via D-Bus) as soon as the application's name is registered on the D-Bus session. I'm mocking the application using python-dbusmock, but it's not trivial because, for the way that mock objects are build, python-dbusmock first registers the service name (and it couldn't be otherwise, since we control the mock object over D-Bus), and only then we can add the objects/methods over D-Bus. This indeed causes a problem in my tests, because the service under tests will invoke the method on the application before the AddMethod() call has been done.
I thought of using a template, but since the object path, interface name and method name of the applications are not known in advance (they are read from the application's .desktop file), we would have to generate the template dynamically, and it makes the tests more complex then what they ought to be.
So I went for another solution, where I start the mock object under a different bus name (the original name with a "XXX" appended to it), add the application's method, and then register the real name. I do this last bit in this way:
reg_interface, reg_method = ('test.mock', 'RegisterName')
mock_iface.AddMethod(reg_interface, reg_method, 's', '',
'self.bus_name.get_bus().request_name(args[0])')
reg_iface = dbus.Interface(bus.get_object(reg_bus_name, object_path), reg_interface)
reg_iface.RegisterName(bus_name)This works fine, but I don't like that it uses internal variables of the mock object (that self.bus_name.get_bus() part in the RegisterName method's implementation). And it seems to me that this functionality might be useful to other people as well.
What do you think about adding a RegisterBusName method to the mock object? If you agree, I could create a PR, it doesn't sound too hard.