diff --git a/lib/jasmine-jquery.js b/lib/jasmine-jquery.js index d3c3871b..be714133 100644 --- a/lib/jasmine-jquery.js +++ b/lib/jasmine-jquery.js @@ -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 } } }, diff --git a/spec/suites/jasmine-jquery-spec.js b/spec/suites/jasmine-jquery-spec.js index 360776a7..92284a57 100644 --- a/spec/suites/jasmine-jquery-spec.js +++ b/spec/suites/jasmine-jquery-spec.js @@ -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($('
')) + 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($('')) + 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($('')) + expect($('#sandbox div')).toHaveAttr('id', 'notexisting') + //Error: Expected attribute value "notexisting" but got "actual" + }); + }); +});