VirtualEntityService + @model = error

Hi there everyone,

I've got a very simple scenario here and I'm not sure why it's not
working.

When I run the following test the testDummyData passes, but the
validateCredentialsShouldReturnBoolean function errors out with:

"The ENTITYNAME parameter to the new function is required but was not
passed in."

*Stack trace at the end of this post

I assume it's because the @model is not creating a true version of the
Service.. but I think I want it to as I'm trying to test (non-VES)
functions

Any help appreciated,

Cheers
Steve

PartyServiceTest -------------------------------------------------
component extends="coldbox.system.testing.BaseModelTest"
model="model.services.users.PartyService"
{
  void function setup(){
    //Call the super setup method to setup the app.
    super.setup();

    // Your own setup here if needed
  }

  function validateCredentialsShouldReturnBoolean()
  {
    party = model.new();
    party.setUsername("username");
    party.setPassword("password");

    model.save(party);

    var partyValid = model.validatePartyCredentials(party);

  }

  function testDummyData()
  {
    data = model.getDummyData();
    expected = {
        1 = 1,
        a = "a",
        now = now()
      };
    assertEquals(expected, data, "Dummy data returns the wrong data");
  }

}
end -------------------------------------------------

PartyService ------------------------------------
component extends="coldbox.system.orm.hibernate.VirtualEntityService"
singleton{

  public PartyService function init(){

    // init super class
    super.init(entityName="Party");

    // Use Query Caching
      setUseQueryCaching( false );
      // Query Cache Region
      setQueryCacheRegion( 'ORMService.defaultCache' );
      // EventHandling
      setEventHandling( true );

      return this;
  }

  public boolean function validatePartyCredentials(Party party)
  {
    return true;
  }

  public struct function getDummyData()
  {
    return
      {
        1 = 1,
        a = "a",
        now = now()
      };
  }

}
end -------------------------------------------

stacktrace ---------------------------------
coldfusion.runtime.MissingArgumentException: The ENTITYNAME parameter
to the new function is required but was not passed in. at
coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:523) at
coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2547) at
coldfusion.runtime.SuperScope.invoke(SuperScope.java:18) at
coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2301) at
cfVirtualEntityService2ecfc1199061123$funcNEW.runFunction(C:\inetpub
\wwwroot\coldbox\system\orm\hibernate\VirtualEntityService.cfc:93) at
coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472) at
coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:
405) at coldfusion.runtime.UDFMethod
$ArgumentCollectionFilter.invoke(UDFMethod.java:368) at
coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:
55) at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220) at
coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:490) at
coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:336) at
coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2360) at
cfPartyServiceTest2ecfc713101611$funcVALIDATECREDENTIALSSHOULDRETURNBOOLEAN.runFunction(C:
\Users\scowling\workspace\hmri_core2\test\unit\users
\PartyServiceTest.cfc:14) ... snip
end stacktrace --------------------------

Hi Stephen,

The issue is that the model test case will mock and prepare the model object for you, but you need to call its constructor. Therefore, you are trying to utilize a non- initialized object.

In your setup() you will need to call the init() with the appropriate params

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Thanks for your reply Luis.

I've been going over this and modifying things to see if I can get it
to work so your instructions helped understand what is happening under
the hood.

I was under the impression that I didn't need to do anything further
to initialise Coldbox - with the init() in place I am receiving the
following error on this line:

party = model.new();

Framework: ColdBox Controller Not Found The coldbox main controller
has not been initialized

I'm obviously missing something simple - I may have even removed
something in my fruitless attempt to get this working.

Cheers
Steve

See below for the current code.

Given our different timezones this discussion is a bit out of whack,
apologies in advance.

I guess my question has morphed from an error to a general conceptual
one.

I'm trying to write a test to test my PartyService, so I'm creating a
mock Party to use in those tests. But I'm not grokking the testing/
mocking paradigm for how this is actually testing my code since I'm
mocking methods on the Service I'm trying to test - ie/ get, new, save

I completely understand the need to test.
I understand how to test business logic within a model/BO
I understand the need to not actually go off to the database in my
tests - I'm not testing the DB

But.. I want to test a non-CRUD method like validatePartyCredentials
(and this is purely POC code just FYI) but I can't because it wants to
actually load a real Party.
And if I mock the method - then how is that helping me test the actual
code? confused.

I'm aware this has now moved out of the realm of Coldbox - and I'm
happy to move it over to the mxunit group if asked to.

There seems to be a gap in my knowledge.. Hoping someone can bridge it
for me.

Cheers
Steve

PartyServiceTest ------------------
component extends="coldbox.system.testing.BaseModelTest"
model="model.services.users.PartyService" appMapping="/"
{

  void function setup(){
    //Call the super setup method to setup the app.
    super.setup();

    partyToMock = new model.users.Party();
    partyToMock.setId(1);
    partyToMock.setUsername("test_username");
    partyToMock.setPassword("password");
    mockParty = getMockBox().prepareMock(partyToMock);
    partyService = model.init();
    partyService.$("get", mockParty);
    partyService.$("new", mockParty);
    partyService.$("save");

  }

  function testGetPartyById()
  {
    party = partyService.get(id=1);
    debug(party);
    assertEquals(mockParty.getId(),party.getId());
  }

  function testValidatePartyCredentials()
  {
    validUser = partyService.validatePartyCredentials(partyToMock);
    assertTrue(validUser, "Party not validated");
  }

  function validateCredentialsShouldReturnBoolean()
  {
    party = partyService.new();
    party.setUsername("username");
    party.setPassword("password");

    partyService.save(party);

    var partyValid = partyService.validatePartyCredentials(party);
    assertEquals(true,partyValid);
  }

  function testDummyData()
  {
    data = partyService.getDummyData();
    expected = {
        1 = 1,
        a = "a",
        now = now()
      };
    assertEquals(expected, data, "Dummy data returns the wrong data");
  }

}

end PartyServiceTest---------------

PartyService-----------------------
component extends="coldbox.system.orm.hibernate.VirtualEntityService"
singleton{

  public PartyService function init(){

    // init super class
    super.init(entityName="Party");

    // Use Query Caching
      setUseQueryCaching( false );
      // Query Cache Region
      setQueryCacheRegion( 'ORMService.defaultCache' );
      // EventHandling
      setEventHandling( true );

      return this;
  }

  public boolean function validatePartyCredentials(model.users.Party
party)
  {
    var validUser = false;
    var loadParty = findWhere({username=party.getUsername()});
    if(isDefined("loadParty"))
    {
      if(party.getPassword() == loadParty.getPassword())
      {
        validUser = true;
      }
    }
    return validUser;
  }

  public struct function getDummyData()
  {
    return
      {
        1 = 1,
        a = "a",
        now = now()
      };
  }

}
end PartyService-------------------

Got it. Yes the new method sends an interception call. Usually you would disable event handling.

Init(eventhandling=false)

Thanks Luis - still not working as I thought it would (the
eventhandling argument doesn't seem to change anything) - so I've
changed direction for the time being and started using a
NamingStrategy to create test tables and I'll populate that with test
data. Pretty sure I won't bother testing the BaseORMService methods -
I'm assuming you've got them well and truly covered :smiley:

Thanks again
Steve

Weird, will check that, but I test my orm services all the time with that’

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano