小编mat*_*uer的帖子

模拟python函数具有多个返回值

我有一个返回多个值的python函数.我的功能:

def myExampleFunction(a,b)
    # here is my code
    return name, number1, number2


def FunctionIWantToTest(self):
    # here is my code
    myName, myNumber1, myNumber2 = self.myExampleFunction(a,b)
Run Code Online (Sandbox Code Playgroud)

我想将自己的值赋予FunctionIWantToTest返回的值.所以,我试图用nosetest测试第二个函数,但我不知道如何模拟myExampleFunction的返回.

我试过这个:

def test_mytest(self):
    [...]
    c.myExampleFunction = Mock()
    c.myExampleFunction.side_effect = ["myName", 100, 200]
    [...]
Run Code Online (Sandbox Code Playgroud)

但它不起作用.当我启动nosetests时,我读到了这条消息:

ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)

任何的想法?我使用python 2.7.3

python mocking nose

7
推荐指数
2
解决办法
9767
查看次数

如何修改for循环中的切片?

我的阅读清单中有一篇文章.每篇文章都有属性"FeedURL",其中包含文章来源的网址.当我取消订阅Feed时,我希望能够删除包含该Feed的每篇文章.

type Article struct {
    FeedURL string
    URL     string // should be unique
    // ... more data
}

func unsubscribe(articleList []Article, url string) []Article {
   // how do I remove every Article from articleList that contains url?
}

func main() {
    myArticleList := []Article{
        Article{"http://blog.golang.org/feed.atom", "http://blog.golang.org/race-detector"},
        Article{"http://planet.python.org/rss20.xml", "http://archlinux.me/dusty/2013/06/29/creating-an-application-in-kivy-part-3/"},
        Article{"http://planet.python.org/rss20.xml", "http://feedproxy.google.com/~r/cubicweborg/~3/BncbP-ap0n0/2957378"},
        // ... much more examples
    }

    myArticleList = unsubscribe(myArticleList, "http://planet.python.org/rss20.xml")

    fmt.Printf("%+v", myArticleList)
}
Run Code Online (Sandbox Code Playgroud)

解决这个问题的有效方法是什么?

起初我的代码看起来像这样取消订阅:

func unsubscribe(articleList []Article, url string) []Article {
    for _, article := range articleList {
        if …
Run Code Online (Sandbox Code Playgroud)

go

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

标签 统计

go ×1

mocking ×1

nose ×1

python ×1