简体   繁体   English

rails 3.2和机械师问题

[英]rails 3.2 and machinist issues

I've just upgraded to Rails 3.2.1 with Ruby 1.9.3-p0 and I'm using Machinist 2.0. 我刚刚使用Ruby 1.9.3-p0升级到Rails 3.2.1,并且正在使用Machinist 2.0。 Before updating a large project all my tests passed. 在更新大型项目之前,我所有的测试都通过了。 The problem I"m having is when I create a blueprint within a 'let' call in my rspec tests and then refer to it in a before do block. 我遇到的问题是当我在rspec测试中的“ let”调用中创建一个蓝图,然后在before do块中引用它时。

  let (:new_post) {Post.make!}

  before do
    Post.stub!(:new).and_return(new_post)
  end

This used to work, and now I get the following error: 这曾经可以工作,现在出现以下错误:

  1) PostsController GET index assigns all posts as @posts
     Failure/Error: let (:new_post) {Post.make!}
     NoMethodError:
       undefined method `title=' for nil:NilClass
     # ./spec/support/blueprints.rb:22:in `block in <top (required)>'
     # ./spec/controllers/posts_controller_spec.rb:37:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/posts_controller_spec.rb:40:in `block (2 levels) in <top (required)>'

Here is my blueprint: 这是我的蓝图:

require 'machinist/active_record'
Post.blueprint do
  title {"Post"}
  body {"hello world"}
end

For now my work around is to create them using instance variables within the before do block, but it would be nice to use the 'let' calls as it keeps my rspec tests cleaner. 现在,我的解决方法是使用before do块中的实例变量创建它们,但是最好使用“ let”调用,因为这可以使我的rspec测试更加整洁。

Funny, I just ran across the exact same problem, although I'm on Rails 3.2.1, Machinist 2.0, and ruby 1.9.2-p290. 有趣的是,尽管我使用的是Rails 3.2.1,Machinist 2.0和ruby 1.9.2-p290,但我遇到了完全相同的问题。 I think there's a conflict between the execution of the Post.stub(:new) stub method and the Machinist make method, but I haven't dug into the code. 我认为Post.stub(:new)存根方法的执行与Machinist make方法之间存在冲突,但是我没有深入研究代码。

The best solution I've come up with is: 我想出的最好的解决方案是:

  before do
    new_post
    Post.stub!(:new).and_return(new_post)
  end

This will initialize the let (since let is lazy-loaded in rspec) before it gets to the stub method. 这将在进入存根方法之前初始化let(因为let是在rspec中延迟加载的)。 It's hacky, but at least you (and I) can keep the let statement. 这很hacky,但是至少您(和我)可以保留let语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM