私は最初の宝石のスペックを書いています。しかし、私はこの奇妙なエラーで立ち往生しています。私はこのエラーを取得していますrspec
を実行したときに、私のRSpecのための コードがrspecの#<Hash:0x007ff3625a4800>のhttparty未定義メソッド `code 'を取得
describe '#success' do
let(:resp) { {"TwilioResponse"=>{"SMSMessage"=>{"Sid"=>"0d1c0cbfb2b5e8f97dddb4479bdbbc6a", "AccountSid"=>"exotel_sid", "From"=>"/exotel_sid", "To"=>"1234", "DateCreated"=>"2016-07-18 15:35:29", "DateUpdated"=>"2016-07-18 15:35:29", "DateSent"=>nil, "Body"=>"test sms", "Direction"=>"outbound-api", "Uri"=>"/v1/Accounts/exotel_sid/Sms/Messages/0d1c0cbfb2b5e8f97dddb4479bdbbc6a", "ApiVersion"=>nil, "Price"=>nil, "Status"=>"queued"}}} }
before{ allow(Generator::Exotel).to receive(:post).with("/#{Generator::configuration.sid}/Sms/send",
{:body => {:To => 1234, :Body => 'test sms'},:basic_auth=>{:username=>"#{Generator::configuration.sid}", :password=>"#{Generator::configuration.token}"}}).and_return(resp) }
it 'returns response object' do
response = Generator::Exotel.send(:to => 1234, :body => "test sms")
expect(response).to eq ({"Status"=>200, "Message"=>"Success"})
end
end
ある
NoMethodError:
undefined method `code' for #<Hash:0x007ff3625a4800>
私はresponse.code
と呼ばれている
def handle_response(response)
response_base = response['TwilioResponse']
if response_base.include?('RestException')
response_base['RestException']
else
{"Status" => response.code, "Message" => "Success" }
end
end
私はhttpartyを知っているがresponse
を作成する場所です要求のためのオブジェクトと応答コードを返します。しかし、私はどのようにダミーを作成するのですか?response_code
私のテストケースが合格するようになりました。私がここにこだわってから2日近くです。誰でも助けてください。私は本当にルビーと初めての仕様を書くのは初めてです。どんな助けもありがとう。
更新からresponse.inspect
> Generator::Exotel.send(:to => 9030435595, :body => 'jsdhgjkdfg')
のために、それは応答
> #<HTTParty::Response:0x7fb8c02f93d0 parsed_response={"TwilioResponse"=>{"SMSMessage"=>{"Sid"=>"d6ee0650072c82941ad2f06746d14ab4", "AccountSid"=>"sinscary", "From"=>"/sinscary", "To"=>"9030435595", "DateCreated"=>"2016-07-21 19:56:07", "DateUpdated"=>"2016-07-21 19:56:07", "DateSent"=>nil, "Body"=>"jsdhgjkdfg", "Direction"=>"outbound-api", "Uri"=>"/v1/Accounts/sinscary/Sms/Messages/d6ee0650072c82941ad2f06746d14ab4", "ApiVersion"=>nil, "Price"=>nil, "Status"=>"queued"}}}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-type"=>["application/xml"], "date"=>["Thu, 21 Jul 2016 14:26:07 GMT"], "server"=>["Apache/2.2.29 (Amazon)"], "x-powered-by"=>["PHP/5.3.28"], "content-length"=>["542"], "connection"=>["Close"]}>
どこかにあなたはxxxはハッシュである 'xxx.code'を呼んでいる、あなたが呼び出しされるべきであるとその可能性'xxx [:code]'。最小、完全、および検証可能な例を提供してください:http://stackoverflow.com/help/mcve – Yule
はい@Yule私は私のコードで 'response.code'を呼び出しています。私は私の質問を更新しました。 – Sinscary
私は 'xxx [:code]'を提案してみましたが、ハッシュにそのようなキーがないので 'nil'を返します。 – Sinscary