Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对weixin_rails_middleware Rspec测试的时候, WeixinRailsMiddleware::WeixinController#default_url_options 报错 #39

Closed
chanshunli opened this issue May 6, 2015 · 3 comments

Comments

@chanshunli
Copy link

我改了default_url_options 方法,测试是通过的(否则是@weixin_message 为 nil ,FromUserName不存在):
https://github.com/lanrion/weixin_rails_middleware/blob/master/app/controllers/weixin_rails_middleware/weixin_controller.rb#L66-L68

      def default_url_options(options={})
        #{ weichat_id: @weixin_message.FromUserName }
        { weichat_id: "o17q2s7mMOrwcFC8WT1BFgBJzAPE"}  # 我的修改
      end

routes.rb

post 'reply' =>  'myweixin#reply'

myweixin_controller_spec.rb

it "...." do
scaner = "<xml>
  <ToUserName>gh_7376db11d3a2</ToUserName>
  <FromUserName>o17q2s7mMOrwcFC8WT1BFgBJzAPE</FromUserName>
  <CreateTime>1430825981</CreateTime>
  <MsgType>event</MsgType>
  <Event>SCAN</Event>
  <EventKey>123</EventKey>
 <Ticket>gQEl8ToAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3IwaE9VN2prb3pIYWgtdE4zMllUAAIEqKtIVQMEgDoJAA</Ticket> </xml>"
     post "reply", scaner
# 得到 response 的测试返回是正确的: 
#@weixin_message
#=> #<WeixinRailsMiddleware::Message:0x007f9a4c0e8560
# @source=
#  #<OpenStruct ToUserName="gh_7376db11d3a2", FromUserName="o17q2s7mMOrwcFC8WT1BFgBJzAPE", CreateTime="1430810714", MsgType="text", Content="Cvvc", MsgId="6145285223601690279">>

end

我不修改 default_url_options 方法,请问如何测试通过?

@lanrion

@lanrion
Copy link
Owner

lanrion commented May 6, 2015

param_xml = request.body.read
xml 字符串,放入body内。

@lanrion lanrion closed this as completed May 8, 2015
@chanshunli
Copy link
Author

@lanrion
请问在哪里写 request.body = " xml...." ? 但问题是,在测试中,还没有等生成 @weixin_message,就先执行了default_url_options 方法的 @weixin_message.FromUserName 了 ,所以测试才会报错的.

要不这样改 ?

  before(:each) do
    WeixinRailsMiddleware::WeixinController.class_eval do
      protected
      def default_url_options(options={})
        { weichat_id: "o17q2s7mMOrwcFC8WT1BFgBJzAPE"}
      end
    end
  end

@lanrion
Copy link
Owner

lanrion commented May 11, 2015

这个还真没测试过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants