Factory_girl 记录在哪里?

发布于 2024-08-26 18:38:11 字数 1157 浏览 5 评论 0原文

我正在尝试通过 WatirRSpec 执行集成测试。因此,我在 /integration 中创建了一个测试文件并编写了一个测试,该测试通过 factory_girl 将测试用户添加到基础中。

问题是 - 我实际上无法使用我的测试用户执行登录。我编写的测试如下所示:

...

before(:each)
  @user = Factory(:user)
  @browser = FireWatir::Firefox.new
end

it "should login"
  @browser.text_field(:id, "username").set(@user.username)
  @browser.text_field(:id, "password").set(@user.password)
  @browser.button(:id, "get_in").click
end

...

当我开始测试并在浏览器中看到“性能”时,它总是会引发用户名无效错误。

我已经开始调查,并做了一个小动作。首先,我开始怀疑工厂是否真的在数据库中创建了用户。因此,在立即调用工厂之后,我放置了一些 puts User.find 内容,只是为了发现用户实际上在数据库中。好的,但由于用户仍然无法登录,我决定亲眼看看他是否出现在数据库中。

我在工厂调用后立即添加了一个 sleep ,然后去查看目前数据库中的内容。看到用户实际上失踪了,我感到很震惊!怎么会?不过,当我尝试在代码中输出用户时,他实际上是从某个地方获取的。 那么,factory_girl 在运行时生成的记录在哪里?test 还是 dev DB?我不明白。

我已经检查了 10 次是否在 test 模式下运行我的 Mongrel(这重要吗?我认为它很重要,因为我正在尝试调整集成测试)以及我的 database.yml 保存正确的连接特定数据。

我正在使用 authlogic,如果这可以提供任何线索(不,放置 activate_authlogic 在这里不起作用)。

I'm trying to perform an integration test via Watir and RSpec. So, I created a test file within /integration and wrote a test, which adds a test user into a base via factory_girl.

The problem is — I can't actually perform a login with my test user. The test I wrote looks as following:

...

before(:each)
  @user = Factory(:user)
  @browser = FireWatir::Firefox.new
end

it "should login"
  @browser.text_field(:id, "username").set(@user.username)
  @browser.text_field(:id, "password").set(@user.password)
  @browser.button(:id, "get_in").click
end

...

As I'm starting the test and see a "performance" in browser, it always fires up a Username is not valid error.

I've started an investigation, and did a small trick. First of all I've started to have doubts if the factory actually creates the user in DB. So after the immediate call to factory I've put some puts User.find stuff only to discover that the user is actually in DB. Ok, but as user still couldn't have logged in I've decided to see if he's present in DB with my own eyes.

I've added a sleep right after a factory call, and went to see what's in the DB at the moment. I was crushed to see that the user is actually missing there! How come? Still, when I'm trying to output a user within the code, he is actually being fetched from somewhere. So where does the records, made by factory_girl within a runtime lie? Is it test or dev DB? I don't get it.

I've 10 times checked if I'm running my Mongrel in test mode (does it matter? I think it does, as I'm trying to tun an integration test) and if my database.yml holds the correct connection specific data.

I'm using an authlogic, if that can give any clue (no, putting activate_authlogic doesn't work here).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘香 2024-09-02 18:38:11

不要忘记 RSpec 在运行规范时可能正在使用转换。 RSpec 将在事务中包装规范的执行,并在最后回滚。这意味着您将无法从该事务外部(即从另一个 SQL 连接)看到该记录。

如果您想确保用户记录实际上是由 Factory Girl 创建的,您可以执行以下操作:

before(:each)
  @user = Factory(:user)
  User.find_by_username(@user.username).should_not be_nil
  @browser = FireWatir::Firefox.new
end

Don't forget that RSpec is probably using transations when running the specs. RSpec will wrap the execution of the spec within a transaction and rollback at the end. It means you won't be able to see the record from outside that transaction (i.e. from another SQL connection).

If you want to ensure the user record is actually created by Factory Girl, you can do something like:

before(:each)
  @user = Factory(:user)
  User.find_by_username(@user.username).should_not be_nil
  @browser = FireWatir::Firefox.new
end
逆光下的微笑 2024-09-02 18:38:11

不知怎的,解决方案变得很奇怪——我把工厂放在 before(:all) 块中,所有的东西都按预期工作。

Somehow the solution went strange — I put factories to before(:all) block, and all the stuff worked as it should.

鹿童谣 2024-09-02 18:38:11

Factory Girl 将在您的测试数据库中创建临时数据库条目。每次测试后,您的测试数据库都会被清除。

Factory Girl is going to create temporary DB entries in your test database. Your tests database is going to be cleared out after each test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文