Is there any possibility that I can test CakePHP plugins?
All of my core Controller and Model tests are working fine but when I try to test the controller of my plugin it gives me the error:
Trying to get property of non-object
See my code below:
public function testTest() {
$result = $this->testAction("/MyPluginController/trst",array("return"=>"vars"));
debug($result);
}
Any ideas?
thank you in advance
Did you add App::uses at the top?
App::uses('ControllerNameController', 'MyPlugin.Controller');
Related
We are currently using CodeCeption to do some acceptance testing, but I would like to also add unit testing to the current setup.
Has anyone done Unit testing in Zend Framework 2 project using CodeCeption? And if so, how did you set up the environment?
Found this in codeception docs: http://codeception.com/docs/modules/ZF2
There is an example in this github project: https://github.com/Danielss89/zf2-codeception
I solved this problem by using PHPUnit. I followed the Zend PHPUnit guide, http://framework.zend.com/manual/current/en/tutorials/unittesting.html, which only got me so far. But, I was able to bootstrap Zend using a phpunit.xml file and pointing that to _bootstrap.php. Codeception is also able to bootstrap my tests using the _bootstrap.php file. So, now I can run my tests using both:
phpunit --bootstrap tests/unit/_bootstrap.php
and
php vendor/bin/codecept run unit
I was surprised to see how few examples there are for this. I was writing tests for a job queue client. Here is a bit of my test:
protected function setUp()
{
$this->serviceManager = new ServiceManager(new ServiceManagerConfig());
$config = Bootstrap::getConfig();
$this->serviceManager->setService('ApplicationConfig', $config);
$this->serviceManager->get('ModuleManager')->loadModules();
$this->client = $this->serviceManager->get('jobclient');
$this->client->addServers(array(array('ip' => '127.0.0.1', 'port' => '666')));
}
public function testServiceCreateSuccess() {
$this->client->setQueue($this->testData['queue']);
$this->assertEquals($this->client->getQueue(), $this->testData['queue'], 'We get the expected queue');
}
I mocked the job client and had a local config. My _bootstrap.php file looks basically the same as the Zend example except that I am using factories defined in the service config to manage some objects. So, I performed the "setService" method in the test setup rather then in the bootsrap init.
I am new in angular JS
I want test angular-seed sample, e2e test run ok, but i can not test unit test (directive, ...), i get this sample from github, and not change this, but have this problem
i get this error in firebug : ReferenceError: module is not defined
i seen this link but not resolve my problem
Testing Angular Service gives error: No module: ngResource
please help, thanks
Publish your code snippet. That might help me getting the issue.
Anyway, you can instantiate your module in beforeEach method,
beforeEach(function(){module("myApp"); // myApp is your module});
This should be defined before injecting controller. Hope, this might help
I am using grails version 1.3.7. In my application in controller, I am making use of messages.properties and fetching the value of the property as
g.message(code:messageKey, args:msgParamsArr)
But when I started writing unit tests for the action in the controller, it gave me errors.
Please can you help me out to understand how to mock g.message exactly so that the existing code will fetch the message properties from messages.properties only.
You can mock it with:
controller.metaClass.message = { message ->
message.code
}
You can include message.args too if you want to just validate the arguments with .contains().
This works like a champ in Grails 2.4.5:
controller.metaClass.message = { Map attrs -> attrs.toString() }
Am fairly new to the world of testing MVC 4 Web Applications and have been attempting to unit test views and controllers to see whether for a given controller that an action renders a particular view, I have been using MvcContrib TestHelper to try to simply the process of testing the application but so far have been unable to get the test to pass.
When the test is run I receive the error Expected view name was 'index' actual was ''
Currently I am running this test method:
[TestMethod]
public void AMAC_Controller_Renders_Index_View()
{
var builder = new TestControllerBuilder();
var controller = new AMACController();
builder.InitializeController(controller);
var result = controller.Index();
result.AssertViewRendered().ForView("index").WithViewData<AMACEnquiryModel>();
}
the controller and model are both currently in use by the application, was wondering if you could give any advice on how get this test working, I have modified previously when I have done this I get another error that the route name already exists in the collection.
After getting some advice from one of the contributors of the MvcContrib project the reason the test wasn't passing was because I was passing the wrong data into the .ForView(), before I had .ForView("index") where the controller was actually passing View(model) so the value for .ForView() was actually an empty string so the assert now looks like this:
result.AssertViewRendered().ForView("").WithViewData<AMACEnquiryModel>();
Suppose I have an action:
function actionShowItem($id)
{
$item = Item::model()->findByPk($id);
$this->render("showitem",array('model' => $id));
}
What is the simple unit test for this action which will verify text in the view output. Its easy in zend framework without using selenium. We could create fake GET and POST too in zend. But I have not found same examples in Yii. Please suggest.
The Yii PHP framework is very good in many aspects but its very sad that it does not internally support any sort of simulated controller action output testing. It only has selenium based web browser methods. I came to Yii from ZendF and zend does have good testing systems including xpath based assertions. So I had to understand the code flow and code this within my components/Controller.php. It could be done without changing any core yii framework which in my opinion is the charm of Yii.
Every client code has components/Controller.php which is a common base class for all controllers in Yii. And render is a CController method which means I could override it and capture view output for use by the unit test code.
You would need a runmode param (in config/main.php) to identify if you are a testrun or a production. In production output is simply echoed while we cannot echo anything in testrun (Just spoils the unit test report). In the test code you get the output in $render_output on which you can do assert wrapper xpath or strpos checks. This hack is not the best but does the job just fine.
function render($view,$data=null,$return=false)
{
$out = parent::render($view,$data,true);
if(isset(Yii::app()->params['runmode'])
&& Yii::app()->params['runmode'] == 'test')
{
global $render_output;
return $render_output = $out;
}
if($return)
return $out;
else
echo $out;
}