Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Improved error messages for toHaveAttr, will address issue # 184 #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/jasmine-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,20 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toHaveAttr: function () {
return {
compare: function (actual, attributeName, expectedAttributeValue) {
return { pass: hasProperty($(actual).attr(attributeName), expectedAttributeValue) }
var selector = actual.selector
, attribute = $(actual).attr(attributeName)
, result = { pass: hasProperty(attribute, expectedAttributeValue) }
if(result.pass) {
return result
}
if(actual.length === 0) {
result.message = 'Expected to find attribute "'+ attributeName +'" on selector "' + actual.selector + '", but no element found'
} else if (attribute === undefined ) {
result.message = 'Expected to find attribute "'+ attributeName +'" on "' + actual.selector + '"'
} else {
result.message = 'Expected attribute value "' + expectedAttributeValue + '" but got "' + attribute + '"'
}
return result
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions spec/suites/jasmine-jquery-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1774,3 +1774,27 @@ describe("jasmine.Env.equalityTesters_", function () {
})
})
});


xdescribe("failing matchers", function () {
beforeEach(function () {
setFixtures(sandbox())
})
describe("toHaveAttr failure messages", function () {
it("should have error message for toHaveAttr when element not found", function () {
$('#sandbox').append($('<div></div>'))
expect($('#sandbox span')).toHaveAttr('class', 'notexisting')
//Error: Expected to find attribute "class" on selector "#sandbox span", but no element found
});
it("should have error message for toHaveAttr if value does not exist", function () {
$('#sandbox').append($('<div id></div>'))
expect($('#sandbox div')).toHaveAttr('readonly', 'notexisting')
//Error: Expected to find attribute "readonly" on "#sandbox div"
});
it("should have error message for toHaveAttr if value is incorrect", function () {
$('#sandbox').append($('<div id="actual"></div>'))
expect($('#sandbox div')).toHaveAttr('id', 'notexisting')
//Error: Expected attribute value "notexisting" but got "actual"
});
});
});