2

我正在单元测试用例中创建用户配置文件并尝试保存它。这是代码:

def create_user(self, email, user_id, is_admin=False):

    self.testbed.setup_env(
        USER_EMAIL = email,
        USER_ID = user_id,
        USER_IS_ADMIN = '1' if is_admin else '0',
        overwrite = True)
    user = users.get_current_user()
    print "user:", user
    u = UserProf( id=str(user.user_id()),
                  nickname = "Test",
                  email_address = user.email() )
    u.put()

我在执行测试用例时调用此函数:

def test_users(self):

    self.create_user('test@example.com', '123', True)
    result = self.app('/users/')
    self.assertTrue("Test" in result)

u.put() 导致问题。错误:

BadKeyError:实体键与数据存储返回的键不同。预期密钥('UserProf','123'),得到密钥('UserProf','123')

完整的堆栈跟踪,它很大:

https://gist.github.com/rajendrakrp/5705313

我在 net.xml 中没有找到与此错误相关的任何内容。除了一个,他也报告了同样的问题:https ://gist.github.com/sivy/3364880

谢谢。

更新:添加了 UserProf 的模型类。

class UserProf(ndb.Model):

    nickname = ndb.StringProperty(required=True)
    email_address = ndb.StringProperty(required=True)
    is_admin = ndb.BooleanProperty()
    teams = ndb.KeyProperty(repeated=True)
    is_manager = ndb.BooleanProperty()
4

2 回答 2

3

在过去的几个小时里我刚刚遇到了同样的问题,我最终用鼻子盖和标志“--without-sandbox”运行了我的测试,如这个错误问题https://code.google.com/p中所示/nose-gae/issues/detail?id=60

我意识到这是一个老问题,但我想我会发布这个,以防它可以帮助其他任何人测试 ndb 这个错误。

于 2013-09-08T13:07:28.513 回答
0

You have to enable the stubs for the services you want to use in your unit tests, datastore included.

class DemoTestCase(unittest.TestCase):

  def setUp(self):
    # First, create an instance of the Testbed class.
    self.testbed = testbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for use.
    self.testbed.activate()
    # Next, declare which service stubs you want to use.
    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()
于 2015-04-28T21:10:35.933 回答