I have the following classes shown below, legacy code. The goal I want to achieve is to ensure that the delegate method processUser is called with the user data passed. Second, I also want to ensure that the passed in Registration object's doRegister is called. My attemot is shown below for the delegate, but the test does not pass as it says, Too few invocations. I am using Groovy spock for testing version 1.2
class Invoker {
Delegate delegate;
Invoker(Delegate delegate) {
this.delegate = delegate;
}
void invoke(UserData user) {
delegate.processUser(user);
}
}
class Delegate {
private RegistrationService service;
Delegate (RegistrationService r) {
this.service = r;
}
void processUser(UserData data) {
service.doRegistration(data);
}
}
class DelegateSpec extends Specification {
Delegate delegate
RegistrationService registration
Invoker invoker
def setup() {
registration = Mock()
delegate = new Delegate(registration)
Invoker invoker = new Invoker(delegate)
}
def "Invoker should invoke delegate passed to it"() {
given:
UserData u = ....
when:
invoker.invoke(u)
then:
1* delegate.processUser(u)
}
}
First let me provide a fully consistent set of classes in order to be able to compile the application code:
package de.scrum_master.stackoverflow.q59366025;
public class UserData {}
package de.scrum_master.stackoverflow.q59366025;
public class RegistrationService {
public void doRegistration(UserData data) {}
}
package de.scrum_master.stackoverflow.q59366025;
class Delegate {
private RegistrationService service;
Delegate (RegistrationService r) {
this.service = r;
}
void processUser(UserData data) {
service.doRegistration(data);
}
}
package de.scrum_master.stackoverflow.q59366025;
class Invoker {
Delegate delegate;
Invoker(Delegate delegate) {
this.delegate = delegate;
}
void invoke(UserData user) {
delegate.processUser(user);
}
}
Now as for your test, you are making it more complicated than necessary and there is also a logical error:
If the Delegate is not mock or spy, you cannot check interactions like 1 * on it.
So just make it a mock, then you also do not need to inject its RegistrationService dependency anymore - which is the whole point of creating a mock.
package de.scrum_master.stackoverflow.q59366025
import spock.lang.Specification
class DelegateSpec extends Specification {
def delegate = Mock(Delegate)
def invoker = new Invoker(delegate)
def "Invoker should invoke delegate passed to it"() {
given:
def userData = new UserData()
when:
invoker.invoke(userData)
then:
1 * delegate.processUser(userData)
}
}
Related
Is it possible in DryIoc container to figure out whether some singleton has been instantiated?
For instance
var container = new Container();
container.Register<IApplicationContext, ApplicationContext>( Reuse.Singleton );
// var context = container.Resolve<IApplicationContext>();
if ( container.IsInstantiated<IApplicationContext>() ) // Apparently this does not compile
{
// ...
}
// OR
if ( container.IsInstantiated<ApplicationContext>() )
{
// ...
}
There is no way at the moment and no such feature planned. You may create an issue to request this.
But I am wandering why it is needed. Cause singleton provides a guarantee to be created only once, so you may not worry or check for double creation.
Is it for something else?
Update
OK, in DryIoc you may register a "decorator" to control and provide information about decoratee creation, here is more on decorators:
[TestFixture]
public class SO_IsInstantiatedViaDecorator
{
[Test]
public void Test()
{
var c = new Container();
c.Register<IService, X>(Reuse.Singleton);
c.Register<XProvider>(Reuse.Singleton);
c.Register<IService>(
Made.Of(_ => ServiceInfo.Of<XProvider>(), p => p.Create(Arg.Of<Func<IService>>())),
Reuse.Singleton,
Setup.Decorator);
c.Register<A>();
var x = c.Resolve<XProvider>();
Assert.IsFalse(x.IsCreated);
c.Resolve<A>();
Assert.IsTrue(x.IsCreated);
}
public interface IService { }
public class X : IService { }
public class A
{
public A(IService service) { }
}
public class XProvider
{
public bool IsCreated { get; private set; }
public IService Create(Func<IService> factory)
{
IsCreated = true;
return factory();
}
}
}
This example also illustrates how powerful is composition of DryIoc decorators and factory methods.
In Laravel 5.2, I want to unit test my Eloquent User Repository.
class EloquentUserRepository implements UserRepositoryInterface
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function oneUser($id)
{
return $this->user->oneUser($id);
}
}
My test looks like below, with mocking the interface:
class EloquentUserRepositoryTest extends TestCase
{
public function setUp()
{
$this->user = factory(User::class, 1)->create(['name' => 'foo']);
}
/** #test */
public function it_fetch_an_user()
{
$mock = Mockery::mock('App\Repositories\Interfaces\UserRepositoryInterface')
->shouldReceive('oneUser')
->once()
->with($this->user->id)
->andReturn('foo');
App::instance(App\Repositories\EloquentUserRepository::class, $mock);
$userRepository = App::make(App\Repositories\EloquentUserRepository::class);
$this->assertEquals('foo', $userRepository->oneUser($this->user->id)->name);
}
public function tearDown()
{
Mockery::close();
}
}
I get this error:
ErrorException: call_user_func_array() expects parameter 1 to be a valid callback, class 'Mockery\Expectation' does not have a method 'oneUser'
I expect a simulated object that has the method oneUser, but it returns Mockery\Expectation. What do I wrong?
When a new instance of EloquentUserRepository is made, a new user model is created. When you then call the oneUser method for the EloquentUserRepository class a method with the same name is called but on the user model. Therefore it's the user model you need to mock, not the UserRepositoryInterface.
You need to create a new instance of the EloquentUserRepository and send in the user model mock as an argument when it's created as shown below:
class EloquentUserRepositoryTest extends TestCase
{
protected $userMock;
public function setUp()
{
parent::setUp();
$this->userMock = Mockery::mock('User');
$this->userMock->id = 1;
}
/** #test */
public function it_fetch_an_user()
{
$this->userMock->shouldReceive('oneUser')->with($this->userMock->id)->andReturn('foo');
$userRepository = App::make(App\Repositories\EloquentUserRepository::class, array($this->userMock));
$this->assertEquals('foo', $userRepository->oneUser($this->userMock->id));
}
public function tearDown()
{
Mockery::close();
}
}
I am calling a Web Api method inside my Repository layer. Can anyone suggest how to test it using Mocking
If you want to mock the call to a Web API method you will have to abstract the code that is calling it.
So abstract it:
public interface IMyApi
{
MyObject Get();
}
and then you could have a specific implementation of this interface that is using an HttpClient to call the actual API:
public class MyApiHttp: IMyApi
{
private readonly string baseApiUrl;
public MyApiHttp(string baseApiUrl)
{
this.baseApiUrl = baseApiUrl;
}
public MyObject Get()
{
using (var client = new HttpClient())
{
client.BaseAddress = this.baseAddress;
var response = client.GetAsync('/api/myobjects').Result;
return response.Content.ReadAsAsync<MyObject>().Result;
}
}
}
Now your repository layer will simply take this abstraction as constructor argument:
public class Repository: IRepository
{
private readonly IMyApi myApi;
public Repository(IMyApi myApi)
{
this.myApi = myApi;
}
public void SomeMethodThatYouWantToTest()
{
var result = this.myApi.Get();
...
}
}
Next in your unit test it's trivial to mock the access to the API using your favorite mocking framework. For example your the unit test with NSubstitute might look like this:
// arrange
var myApiMock = Substitute.For<IMyApi>();
var sut = new Repository(myApiMock);
var myObject = new MyObject { Foo = "bar", Bar = "baz" };
myApiMock.Get().Returns(myObject);
// act
sut.SomeMethodThatYouWantToTest();
// assert
...
Maybe I am doing this wrong.
I'd like to test the beforeSave method of a model (Antibody). A part of this method calls a method on an associated model (Species). I'd like to mock the Species model but don't find how.
Is it possible or am I doing something that goes against the MVC pattern and thus trying to do something that I shouldn't?
class Antibody extends AppModel {
public function beforeSave() {
// some processing ...
// retreive species_id based on the input
$this->data['Antibody']['species_id']
= isset($this->data['Species']['name'])
? $this->Species->getIdByName($this->data['Species']['name'])
: null;
return true;
}
}
Assuming your Species model in created by cake due to relations, you can simply do something like this:
public function setUp()
{
parent::setUp();
$this->Antibody = ClassRegistry::init('Antibody');
$this->Antibody->Species = $this->getMock('Species');
// now you can set your expectations here
$this->Antibody->Species->expects($this->any())
->method('getIdByName')
->will($this->returnValue(/*your value here*/));
}
public function testBeforeFilter()
{
// or here
$this->Antibody->Species->expects($this->once())
->method('getIdByName')
->will($this->returnValue(/*your value here*/));
}
Well, it depends on the way your 'Species' object is injected.
Is it injected via the constructor ? Via a setter ? Is it inherited ?
Here is an example with a constructor injected object :
class Foo
{
/** #var Bar */
protected $bar;
public function __construct($bar)
{
$this->bar = $bar;
}
public function foo() {
if ($this->bar->isOk()) {
return true;
} else {
return false;
}
}
}
Then your test would be something like this:
public function test_foo()
{
$barStub = $this->getMock('Overblog\CommonBundle\TestUtils\Bar');
$barStub->expects($this->once())
->method('isOk')
->will($this->returnValue(false));
$foo = new Foo($barStub);
$this->assertFalse($foo->foo());
}
The process is quite the same with setter injected objects :
public function test_foo()
{
$barStub = $this->getMock('Overblog\CommonBundle\TestUtils\Bar');
$barStub->expects($this->once())
->method('isOk')
->will($this->returnValue(false));
$foo = new Foo();
$foo->setBar($barStub);
$this->assertFalse($foo->foo());
}
[TestMethod()]
public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled()
{
//Arrange
Messenger.Reset();
MainViewModel target = new MainViewModel();
bool wasCalled = false;
Messenger.Default.Register<NotificationMessage>(this,"Settings",(msg) => wasCalled = true);
//Act
target.ShowSetupCommand.Execute(null);
//Assert
Assert.IsTrue(wasCalled);
}
I see there is an IMessenger interface and I tried to mock it and set Messenger.OverrideDefault to the mock like this:
var mock = new Mock<IMessenger>();
Messenger.OverrideDefault((Messenger)mock.Object);
But I got an invalid cast error. Is the OverrideDefault method not for that purpose or more likely I'm using it incorrectly.
Or would I have an interface for the classes that are receiving the messages and mock those? All I really want to test is that a RelayCommand sends a message when it is called.
I just started to look at this myself. I'm a little surprised that Messenger.OverrideDefault doesn't take an IMessenger as a parameter. You have to inherit Messenger.
I suppose you could create a class that internally uses your mock object and then do a Verify.
[Test]
public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled() {
Messenger.Reset();
MaintenanceViewModel target = new MainViewModel();
IMessenger mockMessenger = MockRepository.GenerateMock<IMessenger>();
mockMessenger.Expect(m => m.Send("Settings"));
TestMessenger testMessenger = new TestMessenger(mockMessenger);
Messenger.OverrideDefault(testMessenger);
bool wasCalled = false;
Messenger.Default.Register<NotificationMessage>(this, "Settings", (msg) => wasCalled = true);
target.ShowSetupCommand.Execute(null);
mockMessenger.VerifyAllExpectations();
}
You may or may not need a stub on the Register method.
The TestMessenger class:
public class TestMessenger : Messenger {
private IMessenger _mockMessenger;
public TestMessenger(IMessenger mock) {
_mockMessenger = mock;
}
public override void Register<TMessage>(object recipient, bool receiveDerivedMessagesToo, Action<TMessage> action) {
_mockMessenger.Register<TMessage>(recipient, receiveDerivedMessagesToo, action);
}
public override void Register<TMessage>(object recipient, Action<TMessage> action) {
_mockMessenger.Register<TMessage>(recipient, action);
}
public override void Send<TMessage, TTarget>(TMessage message) {
_mockMessenger.Send<TMessage, TTarget>(message);
}
public override void Send<TMessage>(TMessage message) {
_mockMessenger.Send<TMessage>(message);
}
public override void Unregister<TMessage>(object recipient, Action<TMessage> action) {
_mockMessenger.Unregister<TMessage>(recipient, action);
}
public override void Unregister<TMessage>(object recipient) {
_mockMessenger.Unregister<TMessage>(recipient);
}
public override void Unregister(object recipient) {
_mockMessenger.Unregister(recipient);
}
}
Another approach that using constructor injection you can see in this answer. I think it's better to use constructor injection instead of using static Messenger.Default. It's more robust approach cause dependency injection providing natural seam with which you can easily substitute dependencies in unit tests. If you try to substitute static member call, then you rely on internal implementation that obviously can change.