我想弄清楚为什么当期望和实际结果看起来相同时,以下规范不会通过
1) Moodle::Client raises moodle error if token is invalid
Failure/Error: expect {
expected Moodle::MoodleError with {"exception"=>"moodle_exception", "errorcode"=>"invalidtoken", "message"=>"Invalid token - token not found"}, got #<Moodle::MoodleError: {"exception"=>"moodle_exception", "errorcode"=>"invalidtoken", "message"=>"Invalid token - token not found"}> with backtrace:
# ./lib/moodle/client.rb:27:in `resolve_request'
# ./lib/moodle/client.rb:11:in `method_missing'
# ./spec/moodle/client_spec.rb:48:in `block (4 levels) in <module:Moodle>'
# ./spec/moodle/client_spec.rb:47:in `block (3 levels) in <module:Moodle>'
# /Users/ryanme/.rvm/gems/ruby-2.2.2@phoenix/gems/vcr-2.9.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call'
# /Users/ryanme/.rvm/gems/ruby-2.2.2@phoenix/gems/vcr-2.9.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'
# /Users/ryanme/.rvm/gems/ruby-2.2.2@phoenix/gems/vcr-2.9.3/lib/vcr.rb:182:in `use_cassette'
# ./spec/moodle/client_spec.rb:46:in `block (2 levels) in <module:Moodle>'
# ./spec/moodle/client_spec.rb:47:in `block (3 levels) in <module:Moodle>'
# /Users/ryanme/.rvm/gems/ruby-2.2.2@phoenix/gems/vcr-2.9.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call'
# /Users/ryanme/.rvm/gems/ruby-2.2.2@phoenix/gems/vcr-2.9.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'
# /Users/ryanme/.rvm/gems/ruby-2.2.2@phoenix/gems/vcr-2.9.3/lib/vcr.rb:182:in `use_cassette'
# ./spec/moodle/client_spec.rb:46:in `block (2 levels) in <module:Moodle>'
代码仍在进行中:
require 'typhoeus'
module Moodle
class Client
attr_reader :web_service_name, :filter_params
def method_missing message, *args, &block
if supports_web_service? message
@web_service_name = message
@filter_params = args.first
resolve_request
else
super
end
end
def supports_web_service? name
true # change to look at configuration for web services
end
def resolve_request
response = request.run
json_body = JSON.parse(response.body)
if json_body['exception']
raise MoodleError, json_body
else
json_body
end
end
def request
Typhoeus::Request.new(Moodle.configuration.api_url,
method: :post,
params: request_params,
headers: { 'Accept' => "json" })
end
def request_params
filter_params.merge!({ moodlewsrestformat: Moodle.configuration.format,
wsfunction: web_service_name,
wstoken: Moodle.configuration.token })
end
end
end
规格规格如下:
it 'raises moodle error if token is invalid' do
Moodle.configure do|c|
c.host = 'http://dev.vle.getsmarter.co.za'
c.token = 'invalidtoken'
end
expected_response = {"exception"=>"moodle_exception","errorcode"=>"invalidtoken","message"=>"Invalid token - token not found"}
VCR.use_cassette("valid_service_with_invalid_token") do
expect {
Moodle::Client.new.core_user_get_users(params)
}.to raise_error(Moodle::MoodleError, expected_response)
end
end
我的问题是为什么它会破裂?它与回溯有关吗?关于如何修复它的任何想法都会很棒。
答案 0 :(得分:2)
raise_error
不会使用哈希值,而是使用错误消息(字符串或正则表达式)。
这样可行:
expect {
Moodle::Client.new.core_user_get_users(params)
}.to raise_error(Moodle::MoodleError, 'Invalid token - token not found')
您可以按passing a block设置其他期望值,例如:
expect {
Moodle::Client.new.core_user_get_users(params)
}.to raise_error(Moodle::MoodleError, 'Invalid token - token not found') { |error|
expect(error.errorcode).to eq('invalidtoken')
}