小编use*_*446的帖子

杀死一个模拟对象:一个Python故事

我在使用Python模拟时遇到了麻烦,并且一直在疯狂.由于担心因为没有足够的研究而投票失败,我已经暂时搁置了这个问题.我在上周累计24小时试图找出如何完成这项工作而不能.

我已经阅读了很多例子并从中创建了这个例子.我知道模拟对象应该易于使用,但这花了太长时间.现在我没时间了.

我想在这里做两件简单的事情:

1.在另一个函数中覆盖request.ok状态代码
2.导致抛出urllib2.HTTPError异常

为方便起见,我已将这两项任务提炼为最简单的示例:

#ExampleModule.py

import requests
import urllib2

def hello_world():    
    try:
        print "BEGIN TRY"
        r = requests.request('GET', "http://127.0.0.1:80")
        print r.ok

        if r.ok:
            print "PATCH 1 FAILED"
        else:
            print "PATCH 1 SUCCESSFUL"

    except urllib2.HTTPError:
        print "PATCH 2 SUCCESSFUL"
        print "EXCEPTION 2 HIT\n"
    else:
        print "PATCH 2 FAILED\n"
Run Code Online (Sandbox Code Playgroud)

#in TestModule.py

import mock
import ExampleModule     

def test_function_try():
    with mock.patch('ExampleModule.hello_world') as patched_request:
        patched_request.requests.request.ok = False
        result = ExampleModule.hello_world()
        print result

def test_function_exception():
    with mock.patch('ExampleModule.hello_world') as patched_exception:
        patched_exception.urllib2.side_effect = HTTPError
        result …
Run Code Online (Sandbox Code Playgroud)

python unit-testing mocking

8
推荐指数
1
解决办法
302
查看次数

在 Rails 路由中定义 url 助手

以前不需要使用 Rails 'url helpers' ,但我正在尝试实现类似Rails: URL/path withparameters的东西。我没有创建任何“资源”,但我的印象是我可以在路由中添加 url_helper 名称,例如:

user_index_path GET 'users/index', to: 'users#index'

但这给出了错误:

undefined method 'GET' for #<ActionDispatch::Routing::Mapper:0x00000007ABCDEF> Did you mean? gets gem

由于我没有太多使用它们,我也对我在这里读到的声明感到困惑,https://blog.arkency.com/all-the-ways-to-generate-routing-paths-in-rails/,说“当然有时你需要 _url 而不是 _path”。我对它们的定义是错误的吗?是的,我阅读了https://guides.rubyonrails.org/routing.html,并看到了有关“3.16 直达路线”的内容,但这与我“看到”的示例并不相符。

ruby-on-rails ruby-on-rails-5

2
推荐指数
1
解决办法
2272
查看次数

带预览和溢出扩展的 Bootstrap 4 折叠面板

我正在尝试通过示例实现这个看似简单的引导程序设置,该示例显示溢出文本的小预览并为溢出添加可扩展面板。问题是它是为 BS3 设计的,而我的卡需要 BS4。它没有按照我的方式工作,我不确定它是否与 BS3/4 不兼容,因为即使我使用 BS3 CDN(<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>),它也不能像示例中那样工作。 .. 甚至没有关闭,没有按钮,没有文字,只有标题“培根 Ipsum”。

老实说,我不太擅长 HTML、JS、CSS 和 CDN……所以我可能会离题。如果我没有使用正确的 CSS 或 JS CDN,请告诉我。我唯一改变的是类的 div id。我已经完成了与手风琴类类似的事情,但没有完全像这样的预览和文本的单个部分(不是一个预览,另一个在扩展时显示)。

index.html : (使用最后加载的 style.css 和正文部分底部的 JS CDN 进行编辑)

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div class="container">
      <div class="module">
        <h3>Bacon Ipsum</h3>
        <p class="collapse" id="collapseExample" aria-expanded="false">
          Bacon ipsum dolor amet doner picanha tri-tip biltong leberkas salami meatball tongue filet mignon
          landjaeger …
Run Code Online (Sandbox Code Playgroud)

css twitter-bootstrap twitter-bootstrap-3 bootstrap-4

1
推荐指数
1
解决办法
9759
查看次数