Rspec 2 和 Rails 3 存根/模拟

发布于 2024-11-14 15:03:58 字数 502 浏览 2 评论 0原文

我目前正在将一个大型应用程序从 Rails 2 迁移到 Rails 3。在我们的功能规范中,我们有很多这样的东西:

@model = Factory :model
@child = Factory :child
Model.stub!(:find).and_return(@model)
Child.stub!(:find).and_return(@child)

...

@child.should_receive(:method).twice

主要问题是,如果我让它访问数据库并获取子级的实际实例,真实的:方法会使测试过于复杂(需要两个大工厂)并且缓慢。

在代码中,我们使用各种方式来获取项目:查找、动态查找器等,

@model = Model.find(1)    
@child = @model.children.find_by_name(name)

您如何建议将此逻辑移至 Rails 3?对另一个存根/模拟库有什么建议吗?

I am currently in the process of migration to rails 3 from rails 2 in a large application. In our functional specs, we have alot of stuff like this:

@model = Factory :model
@child = Factory :child
Model.stub!(:find).and_return(@model)
Child.stub!(:find).and_return(@child)

...

@child.should_receive(:method).twice

The main issue is that if I let it hit DB and get actual instance of child, real :method makes tests too complex (need two big factories) and slow.

In code we use various ways to get items: find, dynamic finders, etc

@model = Model.find(1)    
@child = @model.children.find_by_name(name)

How would you advice to move this logic to rails 3? Any advice on another stubbing/mocking library maybe?

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

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

发布评论

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

评论(1

锦上情书 2024-11-21 15:03:58

通常,您会在控制器规格内模拟模型:

Model.stub!(:find).and_return(mock_model('Model'))
Child.stub!(:find).and_return(mock_model('Child'))

但是,当您的 Rails 3 应用程序的 Gemfile 中有 gem "rspec-rails", "~> 2.0" 时,标准的 Rails 脚手架生成器将使用 rspec 为您生成规范,因此运行 railsgeneratescaffold MyResource 将为您生成一些示例规范。

以下是rails/rspec 将为控制器规格生成的内容的简单注释版本,因此我认为这应该被视为“RSpec 方式”。

describe AccountsController do

  # Helper method that returns a mocked version of the account model.
  def mock_account(stubs={})
    (@mock_account ||= mock_model(Account).as_null_object).tap do |account|
      account.stub(stubs) unless stubs.empty?
    end
  end

  describe "GET index" do
    it "assigns all accounts as @accounts" do
      # Pass a block to stub to specify the return value
      Account.stub(:all) { [mock_account] }
      get :index
      # Assertions are also made against the mock
      assigns(:accounts).should eq([mock_account])
    end
  end

  describe "GET show" do
    it "assigns the requested account as @account" do
      Account.stub(:find).with("37") { mock_account }
      get :show, :id => "37"
      assigns(:account).should be(mock_account)
    end
  end

  describe "GET new" do
    it "assigns a new account as @account" do
      Account.stub(:new) { mock_account }
      get :new
      assigns(:account).should be(mock_account)
    end
  end
end

Normally you would mock the model inside controller specs:

Model.stub!(:find).and_return(mock_model('Model'))
Child.stub!(:find).and_return(mock_model('Child'))

However, when you've got gem "rspec-rails", "~> 2.0" in your rails 3 app's Gemfile, then the standard rails scaffold generator will use rspec to generate specs for you, so running rails generate scaffold MyResource will generate some example specs for you.

The following is a lightly annotated version of what rails/rspec will generate for controller specs, so I suppose this should be considered "The RSpec Way".

describe AccountsController do

  # Helper method that returns a mocked version of the account model.
  def mock_account(stubs={})
    (@mock_account ||= mock_model(Account).as_null_object).tap do |account|
      account.stub(stubs) unless stubs.empty?
    end
  end

  describe "GET index" do
    it "assigns all accounts as @accounts" do
      # Pass a block to stub to specify the return value
      Account.stub(:all) { [mock_account] }
      get :index
      # Assertions are also made against the mock
      assigns(:accounts).should eq([mock_account])
    end
  end

  describe "GET show" do
    it "assigns the requested account as @account" do
      Account.stub(:find).with("37") { mock_account }
      get :show, :id => "37"
      assigns(:account).should be(mock_account)
    end
  end

  describe "GET new" do
    it "assigns a new account as @account" do
      Account.stub(:new) { mock_account }
      get :new
      assigns(:account).should be(mock_account)
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文