php


laravel unit test index function in controlller


this is my index function
public function index($alias, $profileId)
{
$this->setClientAndClientProfile($alias, $profileId);
$routeData = Routedata
::where('client_id', $this->client->id)
->paginate(10);
return view('client.route_data.index', compact('routedata'))->with('client', $this->client)->with('clientProfile', $this->clientProfile);
}
setClientAndClientProfile is function just to check type of user and bring his profile
so how to write a test for this function?
In your case, you can simply assert that the view returned by the route has routedata.
public function testIndex()
{
$this->call('GET', '/path/to/my/controlller/method');
$this->assertViewHas('routedata');
$this->assertViewHas('client');
$this->assertViewHas('clientProfile');
}
However, you could take this one step further and you could make assertions about the type of data that was sent to the view.
First, grab the data:
$routedata = $response->original->getData()['routedata'];
$client = $response->original->getData()['client'];
$clientProfile = $response->original->getData()['clientProfile '];
Now you can test the instances of these variables to ensure they were properly set as well:
$this->assertInstanceOf('\Illuminate\Database\Eloquent\Collection', $routedata);
$this->assertInstanceOf('\App\Client', $client);
$this->assertInstanceOf('\App\ClientProfile', $clientProfile);
All together it would be something like:
public function testIndex()
{
$this->call('GET', '/path/to/my/controlller/method');
$this->assertViewHas('routedata');
$this->assertViewHas('client');
$this->assertViewHas('clientProfile');
$routedata = $response->original->getData()['routedata'];
$client = $response->original->getData()['client'];
$clientProfile = $response->original->getData()['clientProfile '];
$this->assertInstanceOf('\Illuminate\Database\Eloquent\Collection', $routedata);
$this->assertInstanceOf('\App\Client', $client);
$this->assertInstanceOf('\App\ClientProfile', $clientProfile);
}
I made assumptions about the types of $client and $clientProfile, so you should adjust the classes accordingly.
Hopefully this helps.

Related Links

how to assign product views count to its supplier to get most viewed supplier - PHP
Missing code in get-stream tutorial
Opencart 2.x checkout / Success add tracking code for specific product
Get value from internal struct representing a context resource
Changing PHP mail.log path on a shared server
SQLSERV in PHP7 not Working
PHP Memcached expiration time
Missing JSON_PRESERVE_ZERO_FRACTION in php 5.6.8
How do i properly store and retrieve an image file path in phpmyadmin?
install php Zip extension on CentOS 6
How to grab data from post to a class Object
Server Side Events in php send all events at once at the end
php & mysql - Fetch users last paid month and compare it with the current month to send email
login form how to give access only to one device
Modal Confirm Box in Codeigniter PHP
How to Change Pagination into Infinite Scroll in codeigniter

Categories

HOME
memory
lambda
cucumber
redmine
website
vsts-build
portable-class-library
lvm
tibco
mosquitto
phpmqtt
spring-security-oauth2
migrate
fireloop
business-intelligence
jquery-select2-4
mpu6050
stimulsoft
dhcp
dma
string-formatting
nose
android-custom-view
social-tables
data-type-conversion
anova
tableview
weex
amazon-sns
quartz.net
beego
clipboard
history.js
react-bootstrap-table
pyspark-sql
locks
publishing
opnet
nsmutablearray
universal
gnu-classpath
buildforge
orchardcms-1.8
bluemixtools
xvfb
djcelery
django-crispy-forms
fastlane
strapi
text-decorations
gitolite
google-now
netapp
resampling
dbcc
brightscript
ilrepack
watir-webdriver
glimpse
alertify
emf-compare
ltrace
maatwebsite-excel
ndk-build
livereload
bilinear-interpolation
fuzzer
ftp-client
callouts
mathml
unsigned
noclassdeffounderror
highlighting
wif4.5
deeplink
in-app-billing
google-maps-api-2
chicagoboss
enaml
featuretoggle
ngcordova
mobilefirst-server
actionpack
ocmockito
deque
tidyr
mutators
fat32
maven-central
mfmessagecomposeview
accountmanager
apache-shindig
ember-table
ldif
2d-vector
icenium
playorm
clrprofiler
android-dialog
live-wallpaper
system-information
pcspim
jzmq
platform-independent
wiktionary
servercontrol
groovy-console
radscheduler
uipasteboard
sql-parametrized-query
asp.net-mvc-views
webresponse
ixmldomdocument
getimagesize
projectpier
service-factory
webclient.uploaddata
synthesizer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App