I'm trying to use a spy to watch when a functino call is made. I can't get it to work with my code though.
My test
it("calls the totalBogof() function", function() {
spyOn(basket(), "totalBogof");
basket().total();
expect(basket.totalBogof).toHaveBeenCalled();
});
The code I'm trying to test:
basket.prototype = {
total: function(){
var total=0.00;
for(var i=0; i<shoppingBasket.length; i++){
total += shoppingBasket[i].price;
}
$('#total').html('total = '+currency+total.toFixed(2));
this.totalBogof(total);
},
totalBogof: function(total){
totalMinBOGOF = total - this.calcBOGOF();
$('#total-bogof').html('Total - BOGOF = ' + currency + totalMinBOGOF.toFixed(2));
this.totalPercentageDiscount(totalMinBOGOF, 10);
},
}
The error I'm getting is this:
5 specs, 1 failureSpec List | Failures
shoppingBasket calls the totalBogof() function
Error: Expected a spy, but got undefined.
Error: Expected a spy, but got undefined.
at compare (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:3083:17)
at Expectation.toHaveBeenCalled (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1480:35)
at Object.<anonymous> (file:///C:/Users/G/shoppingbasket-jasmine/shopping-basket.js:56:47)
at attemptSync (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1886:24)
at QueueRunner.run (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1874:9)
at QueueRunner.execute (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1859:10)
at Spec.queueRunnerFactory (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:697:35)
at Spec.execute (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:359:10)
at Object.fn (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:2479:37)
at attemptAsync (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1916:24)
Any ideas what I need to do to fix this?
Did you try this? Look at the Spies documentation
it("calls the totalBogof() function", function() {
spyOn(basket, "totalBogof");
basket.total();
expect(basket.totalBogof).toHaveBeenCalled();
});
Related
I would like Cypress to auto-generate an it block for each item in the below hash. When I currently run cypress, it picks up on the second test just fine, but ignores the one with the while loop. How can I resolve this? I'd prefer not to have to write out an explicit it block for each item in the map.
const testDataMappings = {
1: {e2eTestName: 'test-one'},
2: {e2eTestName: 'test-two'},
3: {e2eTestName: 'test-three'},
}
// Does not work
describe('My Tests', function () {
let i = 1;
while (i < testDataMappings.length + 1) {
let entry = testDataMappings[i];
it("Should Do The Thing Correctly For" + entry.e2eTestName, () => {
const standardCaseUrl = Cypress.config().baseUrl + "?profile_id=" + String(i);
cy.visit(standardCaseUrl);
cy.wait(5000);
cy.get('.some-div-class-name').compareSnapshot(entry.e2eTestName, 0.0);
});
i +=1;
}
// works
describe('Another Describe block', function () {
it('Should do the thing', () => {
const standardCaseUrl = Cypress.config().baseUrl + "?profile_id=1";
cy.visit(standardCaseUrl);
cy.wait(5000);
cy.get('.some-div-class-name').compareSnapshot('some-snapshot-name', 0.0);
});
});
});
Console logs don't seem to show up, so don't have much insight into what is happening.
There is an example in the Cypress Real World App, a payment application to demonstrate real-world usage of Cypress testing methods, patterns, and workflows. The example is in the transaction feeds spec and uses lodash each to iterate over a feedViews object and dynamically generate the tests for each feed.
I am making an app in Laravel using Vue.js. I would like to wait two seconds when a method is triggered and then execute a store action. However, when I implement this I receive an error.
Here is my code:
.listen('TeamLeaving', e => {
setTimeout(function() {
axios.get('/api/team/' + e.team.id + '/pulse').then(response => {
if (response.data === 0) {
// here is where it messes up
this.$store.commit('team/REMOVE_TEAM', e.team)
}
})
}, 2000)
// this.$store.commit('team/REMOVE_TEAM', e.team);
})
However I get an error:
Uncaught (in promise) TypeError: Cannot read property 'commit' of undefined
When I do the commit outside of the setTimeout it works just fine. So I am assuming there is a problem inside the setTimeout. Could someone help me maneuver this?
This post might help you: how to set timeout in a vueJs method
The important bit:
this in anonymous function is attached to that anonymous function
not to your main function
You can try this:
.listen('TeamLeaving', (e) => {
let vm = this;
setTimeout(function () {
axios.get('/api/team/'+ e.team.id + '/pulse')
.then(response => {
if (response.data === 0) {
//here is where it messes up
vm.$store.commit('team/REMOVE_TEAM', e.team)
}
});
}, 2000);
// this.$store.commit('team/REMOVE_TEAM', e.team);
});
I'm trying to use page objects in Nightwatch js and am creating my own commands in that. For some reason now Nightwatch doesn't seem to recognise standard commands on browser and give me a type error on different commands. What am I doing wrong with my code?
I'm tried different things here already, for example adding 'this' or 'browser' in front of the command, which didn't help. My code has gone through many versions already I am not even sure anymore what all I've tried after Googling the error.
My pageObject:
const homePageCommands = {
deleteAllListItems: function (browser) {
browser
.elements('css selector', '#mytodos img', function (res) {
res.value.forEach(elementObject => {
browser.elementIdClick(elementObject.ELEMENT);
});
})
.api.pause(1000)
return this;
}
};
module.exports = {
url: "http://www.todolistme.net"
},
elements: {
myTodoList: {
selector: '#mytodos'
},
deleteItemButton: {
selector: 'img'
}
},
commands: [homePageCommands]
};
My test:
require('../nightwatch.conf.js');
module.exports = {
'Validate all todo list items can be removed' : function(browser) {
const homePage = browser.page.homePage();
homePage.navigate()
.deleteAllListItems(homePage)
// I have not continued the test yet because of the error
// Should assert that there are no list items left
}
};
Expected behaviour of the custom command is to iterate over the element and click on it.
Actual result:
TypeError: browser.elements is not a function
at Page.deleteAllListItems (/pageObjects/homePage.js:18:14)
at Object.Validate all todo list items can be removed (/specs/addToList.js:8:14)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
And also:
Error while running .navigateTo() protocol action: invalid session id
Looks like you need to pass browser to the deleteAllListItems function instead of homePage on this line:
homePage.navigate()
.deleteAllListItems(homePage)
In Jasmine 1.3, we had this option to the get current spec and suite names:
describe("name for describe", function () {
it("name for it", function () {
console.log(this.suite.getFullName()); // would print "name for describe"
console.log(this.description); // would print "name for it"
});
});
This does not longer work in Jasmine 2.x.
Anyone knows how to fetch those?
Thanks.
I add a new jasmine reporter, then get the spec name without define N variable on each spec. Hope can help, thanks.
var reporterCurrentSpec = {
specStarted: function(result) {
this.name = result.fullName;
}
};
jasmine.getEnv().addReporter(reporterCurrentSpec);
The reason this no longer works is because this is not the test. You can introduce a subtle change to your declarations however that fix it. Instead of just doing:
it("name for it", function() {});
Define the it as a variable:
var spec = it("name for it", function() {
console.log(spec.description); // prints "name for it"
});
This requires no plug-ins and works with standard Jasmine.
As far as Jasmine 2 is concerned currentSpec is discontinued on purpose. However there is a custom plugin/library developed that is based on jasmine reporter plugin which you can use. Here's the Link. Hope it helps with your requirement.
Its very simple to use, install the package with npm command -
npm install -g jasmine-test-container-support
Get the test container support by writing below lines before your describe or test suite -
var JasmineTestContainerSupport = window.JasmineTestContainerSupport || require('jasmine-test-container-support');
JasmineTestContainerSupport.extend(jasmine);
Later use the test container in your spec's to get its description -
var specDesc = jasmine.getEnv().getTestContainer();
Hope this helps.
var currentSpecName = describe('Test1', function() {
var currentStepName = it("Step1", function(){
console.log(currentStepName.description); // Prints It Name
console.log(currentSpecName.getFullName()); //Prints Describe Name
});
});
This worked for me in jasmine 3.5+
I know this is a relatively old question but found something which worked for me
describe('Desc1',() => {
afterEach(() => {
const myReporter = {
specDone: (result) => {
console.log('Spec FullName: ' + result.fullName);
console.log('Spec Result: ' + result.status);
}
};
jasmine.getEnv().addReporter(myReporter);
});
})
Credit for the solution : https://groups.google.com/g/jasmine-js/c/qqOk6Nh7m4c/m/Nyovy2EjAgAJ
This is probably a bit late but you can get the suite name outside the spec.
Please try the following code:
describe("name for describe", function () {
console.log(this.getFullName()); // would print "name for describe"
it("name for it", function () {
//Your test spec
});
});
I have an issue with setting an additional fields to the Parse query result set. An example code is below. Thanks in advance!
When I get the result set from a query in the server-side function I try to run the following code that doesn't work as expected.
querySubscribes.find(
{
success:function(subscribes)
{
for(var i = 0, len = subscribes.length; i < len;i++ ){
subscribes[i]['test'] = 1; // this doesn't work. seems like this object is kind of immutable!
}
response.success(subscribes);
},
error: function(err){
response.error(err);
}
});