当我测试隐藏菜单的功能时,在切换菜单时会出现错误消息“ Expected false is true”。显示菜单正常,但另一个菜单不行。我需要通过所有测试。这是代码。如有必要,我将上传项目的其余部分。请不要介意评论。
/* feedreader.js
*
* This is the spec file that Jasmine will read and contains
* all of the tests that will be run against your application.
*/
/* We're placing all of our tests within the $() function,
* since some of these tests may require DOM elements. We want
* to ensure they don't run until the DOM is ready.
*/
$(function() {
/* This is our first test suite - a test suite just contains
* a related set of tests. This suite is all about the RSS
* feeds definitions, the allFeeds variable in our application.
*/
describe('RSS Feeds', function() {
/* This is our first test - it tests to make sure that the
* allFeeds variable has been defined and that it is not
* empty. Experiment with this before you get started on
* the rest of this project. What happens when you change
* allFeeds in app.js to be an empty array and refresh the
* page?
*/
it('are defined', function() {
expect(allFeeds).toBeDefined();
expect(allFeeds.length).not.toBe(0);
});
/* TODO: Write a test that loops through each feed
* in the allFeeds object and ensures it has a URL defined
* and that the URL is not empty.
*/
it('url is defined', function() {
allFeeds.forEach(function(feed) {
feedLink = feed.url;
expect(feedLink).toBeDefined();
expect(feedLink.length).not.toBe(0);
});
});
/* TODO: Write a test that loops through each feed
* in the allFeeds object and ensures it has a name defined
* and that the name is not empty.
*/
it('name is defined', function() {
allFeeds.forEach(function(feed) {
feedName = feed.name;
expect(feedName).toBeDefined();
expect(feedName.length).not.toBe(0);
});
});
});
/* TODO: Write a new test suite named "The menu" */
/* TODO: Write a test that ensures the menu element is
* hidden by default. You'll have to analyze the HTML and
* the CSS to determine how we're performing the
* hiding/showing of the menu element.
*/
describe('Menu', function () {
it('menu hidden', function () {
expect($('.menu-hidden').is(':visible')).toBe(true);
});
/* TODO: Write a test that ensures the menu changes
* visibility when the menu icon is clicked. This test
* should have two expectations: does the menu display when
* clicked and does it hide when clicked again.
*/
it('toggle on', function () {
$('.menu-icon-link').trigger('click');
expect($('.menu-hidden').is(':visible')).toBe(false);
});
it('toggle off', function () {
$('.menu-icon-link').trigger('click');
expect($('.menu-hidden').is(':visible')).toBe(true);
});
});
/* TODO: Write a new test suite named "Initial Entries" */
/* TODO: Write a test that ensures when the loadFeed
* function is called and completes its work, there is at least
* a single .entry element within the .feed container.
* Remember, loadFeed() is asynchronous so this test will require
* the use of Jasmine's beforeEach and asynchronous done() function.
*/
describe('Initial Entries', function() {
beforeEach(function (done) {
loadFeed(0, function() {
done();
});
});
it('entry element', function () {
expect($('.feed .entry').length).toBeGreaterThan(0);
});
});
/* TODO: Write a new test suite named "New Feed Selection" */
describe('New Feed Selection', function() {
var testFeed;
/* TODO: Write a test that ensures when a new feed is loaded
* by the loadFeed function that the content actually changes.
* Remember, loadFeed() is asynchronous.
*/
beforeEach(function(done) {
loadFeed(0, function() {
testFeed = $('.feed').html();
loadFeed(1, done);
});
});
it('if the feeds are different', function() {
expect($('.feed').html()).not.toEqual(testFeed);
});
});
}());
答案 0 :(得分:0)
您的“打开”和“关闭”规格完全相同。单元测试是独立运行的,因此即使您的第一个测试将按钮打开了,该状态也不会延续到下一个测试中。第二个测试应该更像这样:
it('toggle off', function () {
$('.menu-icon-link').trigger('click');
$('.menu-icon-link').trigger('click');
expect($('.menu-hidden').is(':visible')).toBe(true);
});