开发者

Shoulda for Rspec, Rails, outputs there is an error but doesn't output the error itself

Well, I'm doing some testing right with Rails+Rspec+Shoulda.

When I do a test like the following:

context @user do
  describe 'Validation' do
    describe :name
      it { should allow_value('something').for :name }
    end
  end
end

When it fails, Rspec just output:

1) Validation name Valid
   Failure/Error: it { should allow_value(value).for :name }
     Did not expect errors when name is se开发者_运维百科t to "something", got error:
   # ./spec/models/user_spec.rb:4:in `block (3 levels) in <top (required)>'

It even says got error: but it doesn't output it! I actually know there is a validation error there, but I want Rspec to tell me that, how I would know what is failing to validate then?

What am I doing wrong? Is that the expected behaviour? I have to overwrite the helpers?


I dug into the Shoulda code and I found that it doesn't show the errors when checking for positive assert. But them are loaded into the @errors variable. So I just monkey patched the one method that defines the output:

module Shoulda
    module ActiveRecord
        module Matchers
            def failure_message
                "Did not expect #{expectation}, got error: \n#{@expected_message ? @matched_error : @errors.join("\n  ")}"
            end
        end
    end
end

The original said:

"Did not expect #{expectation}, got error: #{@matched_error}"

I saved it to /lib/shoulda/activerecord/matchers.rb and loaded it with config.autoload_paths += Dir["#{config.root}/lib/**/"]

Hope this helps someone with the same issue ^^


Yup welcome to spec testing so you need to recreate the error in console if you want the error, rspec is not a debugger just a test suite.

I run into this a lot

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜