I can't manage to provide code coverage for this $state changing service:
($state) ->
#$inject = ["$state"]
-> $state.go "home.about"
This is my test which fails the last two expects:
mock = require "mock"
describe "goHomeService", ->
goHomeService = $state = undefined
beforeEach mock.module "main"
beforeEach mock.inject (_goHomeService_, _$state_) ->
goHomeService = _goHomeService_
$state = _$state_
it "should chage $state to 'home.about'", ->
spy = jasmine.createSpy $state, "go"
expect($state.current.name).not.toEqual "home.about"
expect($state.go).toEqual jasmine.any Function
goHomeService()
expect($state.current.name).toEqual "home.about"
expect(spy).toHaveBeenCalledWith "home.about"
Related
Suppose I have:
function f {
[CmdletBinding(DefaultParameterSetName='x')]
param(
[Parameter(Mandatory,ParameterSetName='x')]
[Alias('a')]
[int]$Apple,
[Parameter(Mandatory,ParameterSetName='y')]
[Alias('b')]
[int]$Banana,
[Parameter(Mandatory,ParameterSetName='x')]
[Alias('b')]
[int]$Cherry
)
"Apple $Apple"
"Banana $Banana"
"Cherry $Cherry"
}
I'd like to be able to call f -Apple 1 -b 3 because including Apple means I'm certainly using Parameter Set x, but powershell complains that the alias b is declared multiple times.
Is it entirely impossible, or am I just missing a trick?
The non-trivial function I'm trying to write is a convenience wrapper for multiple external functions that have their own aliases, some of which can be the same for different named parameters, but the set of mandatory parameters would never be ambiguous.
I couldn't get it to work using regular params, but I found a workaround by defining Banana and Cherry as dynamic params. This way the alias b is only defined once, so PowerShell won't complain.
function f {
[CmdletBinding(DefaultParameterSetName='x')]
param(
[Parameter(Mandatory, ParameterSetName='x')]
[Alias('a')]
[int]$Apple
)
DynamicParam {
# If param 'Apple' exists, define dynamic param 'Cherry',
# else define dynamic param 'Banana', both using alias 'b'.
if( $PSBoundParameters.ContainsKey('Apple') ) {
$paramName = 'Cherry'
$paramSetName = 'x'
} else {
$paramName = 'Banana'
$paramSetName = 'y'
}
$aliasName = 'b'
$parameterAttribute = [System.Management.Automation.ParameterAttribute]#{
ParameterSetName = $paramSetName
Mandatory = $true
}
$aliasAttribute = [System.Management.Automation.AliasAttribute]::new( $aliasName )
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add( $parameterAttribute )
$attributeCollection.Add( $aliasAttribute )
$dynParam = [System.Management.Automation.RuntimeDefinedParameter]::new(
$paramName, [Int32], $attributeCollection
)
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add($paramName, $dynParam)
$paramDictionary
}
process {
"--- Parameter Set '$($PSCmdlet.ParameterSetName)' ---"
if( $PSBoundParameters.ContainsKey('Apple') ) {
"Apple $Apple"
}
if( $PSBoundParameters.ContainsKey('Banana') ) {
# Dynamic params require special syntax to read
$Banana = $PSBoundParameters.Banana
"Banana $Banana"
}
if( $PSBoundParameters.ContainsKey('Cherry') ) {
# Dynamic params require special syntax to read
$Cherry = $PSBoundParameters.Cherry
"Cherry $Cherry"
}
}
}
Calling the function:
f -Apple 1 -b 3
f -b 2
Output:
--- Parameter Set 'x' ---
Apple 1
Cherry 3
--- Parameter Set 'y' ---
Banana 2
I am attempting to use New-OktaApp to make a new okta application. It runs without errors, however once it runs powershell fails to run any further and must be forced closed.
Has anyone experienced this before?
If you have used this in the past can you show me an example of how would got it to run and produce an app?
Import-Module "pathtomodule\OktaAPI"
Connect-Okta "MyAPIToken" "MyOrg"
New-OktaApp #{
name = "name";
label = "label";
}
There are many examples in the GH site, for example
https://github.com/gabrielsroka/OktaAPI.psm1/blob/master/CallOktaAPI.ps1#L12-L33
function Add-SwaApp() {
$user = Get-OktaUser "me"
# see https://developer.okta.com/docs/api/resources/apps#add-custom-swa-application
$app = #{
label = "AAA Test App"
settings = #{
signOn = #{loginUrl = "https://aaatest.oktapreview.com"}
}
signOnMode = "AUTO_LOGIN"
visibility = #{autoSubmitToolbar = $false}
}
$app = New-OktaApp $app
# see https://developer.okta.com/docs/api/resources/apps#assign-user-to-application-for-sso
$appuser = #{id = $user.id; scope = "USER"}
Add-OktaAppUser $app.id $appuser
}
I'm a bit new to PowerShell and specifically Pester testing. I can't seem to recreate a scenario for the function I am making Pester test.
Here is the code:
$State = Get-Status
if(State) {
switch ($State.Progress) {
0 {
Write-Host "Session for $Name not initiated. Retrying."
}
100{
Write-Host "Session for $Name at $($State.Progress) percent"
}
default {
Write-Host "Session for $Name in progress (at $($State.Progress)
percent)."
}
}
I've mocked Get-Status to return true so that the code path would go inside the if block, but then the result doesn't have any value for $State.Progress.
My test would always go into the default block in terms of code path. I tried
creating a custom object $State = [PSCustomObject]#{Progress = 0} to no avail.
Here is part of my Pester test:
Context 'State Progress returns 0' {
mock Get-Status {return $true} -Verifiable
$State = [PSCustomObject]#{Progress = 0}
$result = Confirm-Session
it 'should be' {
$result | should be "Session for $Name not initiated. Retrying."
}
}
There are a couple of issues:
Per 4c's comments, your Mock may not be being called because of scoping (unless you have a describe block around your context not shown). If you change Context to Describe and then use Assert-VerifiableMocks you can see that the Mock does then get called.
You can't verify the output of code that uses Write-Host because this command doesn't write to the normal output stream (it writes to the host console). If you remove Write-Host so that the strings are returned to the standard output stream, the code works.
You can use [PSCustomObject]#{Progress = 0} to mock the output of a .Progress property as you suggested, but I believe this should be inside the Mock of Get-Status.
Here's a minimal/verifiable example that works:
$Name = 'SomeName'
#Had to define an empty function in order to be able to Mock it. You don't need to do this in your code as you have the real function.
Function Get-Status { }
#I assumed based on your code all of this code was wrapped as a Function called Confirm-Session
Function Confirm-Session {
$State = Get-Status
if ($State) {
switch ($State.Progress) {
0 {
"Session for $Name not initiated. Retrying."
}
100{
"Session for $Name at $($State.Progress) percent"
}
default {
"Session for $Name in progress (at $($State.Progress) percent)."
}
}
}
}
#Pester tests for the above code:
Describe 'State Progress returns 0' {
mock Get-Status {
[PSCustomObject]#{Progress = 0}
} -Verifiable
#$State = [PSCustomObject]#{Progress = 0}
$result = Confirm-Session
it 'should be' {
$result | should be "Session for $Name not initiated. Retrying."
}
it 'should call the verifiable mocks' {
Assert-VerifiableMocks
}
}
Returns:
Describing State Progress returns 0
[+] should be 77ms
[+] should call the verifiable mocks 7ms
I have a Pertl Tk code, I want to close the main window and open another but after the first window is closing again when second window gets open te first window also appears.
The code
use strict;
use Tk;
my $mw;
#Calling the welcome_window sub
welcome_window();
sub welcome_window{
#GUI Building Area
$mw = new MainWindow;
my $frame_header = $mw->Frame();
my $header = $frame_header -> Label(-text=>"Molex Automation Tool");
$frame_header -> grid(-row=>1,-column=>1);
$header -> grid(-row=>1,-column=>1);
my $region_selected = qw/AME APN APS/;
my $frame_sub_header = $mw->Frame();
my $sub_header = $frame_sub_header -> Label(-text=>"Region Selection");
$frame_sub_header -> grid(-row=>2,-column=>1);
$sub_header -> grid(-row=>2,-column=>1);
my $frame_region = $mw->Frame();
my $label_region = $frame_region -> Label(-text=>"Region:");
my $region_options = $frame_region->Optionmenu(
-textvariable => \$region_selected,
-options => [#regions],
);
$frame_region -> grid(-row=>3,-column=>1);
$label_region -> grid(-row=>3,-column=>1);
$region_options -> grid(-row=>3,-column=>2);
my $frame_submit = $mw->Frame();
my $submit_button = $frame_submit->Button(-text => 'Go!',
-command => \&outside,
);
$frame_submit -> grid(-row=>4,-column=>1,-columnspan=>2);
$submit_button -> grid(-row=>4,-column=>1,-columnspan=>2);
MainLoop;
}
#This sub is just made to close the main window created in Welcome_Wiondow() sub and call the second_window()
sub outside{
$mw -> destroy;
sleep(5);
second_window();
}
sub second_window{
my $mw2 = new MainWindow;
my $frame_header2 = $mw2->Frame();
my $header2 = $frame_header2 -> Label(-text=>"Molex Automation Tool");
$frame_header2 -> grid(-row=>1,-column=>1);
$header2 -> grid(-row=>1,-column=>1);
my $frame_sub_header2 = $mw2->Frame();
my $sub_header2 = $frame_sub_header2 -> Label(-text=>"Tasks Region Wise");
$frame_sub_header2 -> grid(-row=>2,-column=>1);
$sub_header2 -> grid(-row=>2,-column=>1);
MainLoop;
}
I have reduced the code and only put the relevant lines. Now please let me know why I can't kill the main window opened in sub welcome_window() in the sub outside(). Currently what it does is it closes the main windows during the sleep command but as soon as I open the main windows of the second_windows, the windows of welcome_window also reappears.
Got the above code working now, there was some issue in the logic
which was calling the welcome_window again. Thank you all for your
help.
You can't have more than one MainWindow. Create a Toplevel window for the initial one, use withdraw to hide the real main window at the start, make it reappear with deiconify:
my $mw = MainWindow->new;
my $tl = $mw->Toplevel;
$tl->protocol(WM_DELETE_WINDOW => sub {
$mw->deiconify;
$tl->DESTROY;
});
$mw->withdraw;
MainLoop();
I am using Facebook with Codeigniter and was working fine
but suddenly stopped working, is facebook changed anything
the facebook function
public function takofacebook($page = TRUE, $name = TRUE) {
if (isset($page) and (($page != TRUE) or ($page != 1)) and isset($name)) {
$data['page'] = $page;
$data['name'] = $name;
}
$this -> load -> library('fb');
if (!$this -> fb -> is_connected()) {
redirect($this -> fb -> login_url(current_url()));
}
$fb_user = $this -> fb -> client -> api('/me');
if (empty($fb_user)) {
$error = "FACEBOOK LOGIN FAILED - USER US EMPTY. FILE: " . __FILE__ . " LINE: " . __LINE__;
$this -> session -> set_flashdata('register_error', $error);
} else {
$this -> user -> set_facebook_id($fb_user['id']);
$user = $this -> user -> get_by_facebook();
if (!empty($user) && !empty($user -> id) && is_numeric($user -> id)) {
//TODO: Make things a bit more secure here
//Login & Redirect home
$this -> _login($user -> id, 'facebook');
$this -> load -> view('users/redirect_home2', $data);
return;
}
}
//Go to the registeration page
$this -> load -> view('users/redirect2', array('method' => 'facebook'));
}
/**
* Logs user in with facebook
*/
//tako facebook
public function zangafacebook() {
$this -> load -> library('fb');
if (!$this -> fb -> is_connected()) {
redirect($this -> fb -> login_url(current_url()));
}
$fb_user = $this -> fb -> client -> api('/me');
if (empty($fb_user)) {
$error = "FACEBOOK LOGIN FAILED - USER US EMPTY. FILE: " . __FILE__ . " LINE: " . __LINE__;
$this -> session -> set_flashdata('register_error', $error);
} else {
$this -> user -> set_facebook_id($fb_user['id']);
$user = $this -> user -> get_by_facebook();
if (!empty($user) && !empty($user -> id) && is_numeric($user -> id)) {
//TODO: Make things a bit more secure here
//Login & Redirect home
$this -> _login($user -> id, 'facebook');
$this -> load -> view('users/redirect_home3');
return;
}
}
//Go to the registeration page
$this -> load -> view('users/redirect3', array('method' => 'facebook'));
}
I tested the official php sdk from Facebook that is on my CodeIgniter site and it works fine.
Is $fb_user empty? Where is the error? Are you using the official PHP SDK for Facebook? There are a lot of variables to figure out what is going wrong here.
Like ifaour suggests, use the official php sdk here if you are not already: https://github.com/facebook/facebook-php-sdk/tree/master/src
Drop the files in the library directory and call it normally
$this -> load -> library('facebook');
Then you can get the facebook data like this:
$fb_user = $this -> facebook -> api('/me');
again I am not sure where your error is so are you missing a parameter here?
$user = $this -> user -> get_by_facebook($fb_user['id']);
This is all I am doing on my site and it is working just fine. This also has the benifit that if Facebook does change anything you just need to download the new SDK and your code should remain unchanged.