1

「Learn Python the hard way」の例 47を実行しています。

そして、これは私のコードです:

from nose.tools import*
from ex47.game import Room

def test_room():

    gold = Room("GoldRoom",

                """This room has gold in it you can grab. there's a door to the north.""")

    assert_equal(gold.name, "GoldRoom")
    assert_equal(gold.paths, {})

def test_room_paths():

    center = Room("Center", "Test room in the center.")
    north = Room("North", "test room in the north.")
    south = Room("south", "test room in the south.")

    center.add_paths({'north': north, 'south': south})
    assert_equal(center.go('north'), north) 
    assert_equal(center.go('south'), south)


def test_map():

    start = Room("Start", "You can go west and down a hole.")
    west = Room("Trees", "There are trees here, you can go east.")
    down = Room("Dungeon", "It's dark down here, you can go up.")

    start.add_paths({'west': west, 'down': down})
    west.add_paths({'east': start})
    down.add_paths({'up': start})

    assert_equal(start.go('west'), west)
    asset_equal(start.go('west').go('east'), start)
    assert_equal(start.go('down').go('up'), start)

ただし、nosetest を使用してこのコードをテストすると、エラーが発生します。

Traceback <most recent call last>:
File "c:\Python31\lib\site-packages\nose\case.py", line 197,in runtest
    self.test<*self.arg>
File "C:\path\Ex47\skeleton\tests\ex47_tests.py", line 28, in test_map
    start.add_paths<{'west': west, 'down':down>}
AttributeError: 'Room' object has no attribute 'add_paths'

Ran 3 tests in 0.030s

FAILED <errors=2>

add_pathsで正常に機能したため、これはばかげているようtest_room_paths()です。

Python 3.1、Windows 7 を使用しています。

game.py必要な場合のコードは次のとおりです。

class Room(object):

    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.paths = {}

    def go(self, direction):
        return self.paths.get(direction, None)


    def add_paths(self, paths):
        self.paths.update(paths)    
4

1 に答える 1

4

「add_paths は test_room_paths() で正常に機能しました」と述べていますが、テストは作成した順序で実行されると想定しています。多くの場合、これらはアルファベット順に実行されます。つまり、test_room_paths はまだ実行されていません。

Room コードが正しくないようです。def add_pathsの下にインデントされています。これは、クラスの別のメソッドではなく、 でローカルに定義された関数であることdef __init__を意味します。クラス内のすべてのキーワードが並んでいることを確認してください。add_paths__init__def

于 2012-07-24T00:41:45.320 に答える