Tit*_*tas 3 mocking find stubbing rspec2 ruby-on-rails-3
我目前正在大型应用程序中从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
Run Code Online (Sandbox Code Playgroud)
主要的问题是,如果我让它命中数据库并获得子实际的实例,那么real:方法会使测试过于复杂(需要两个大工厂)并且速度慢.
在代码中,我们使用各种方式来获取项目:查找,动态查找器等
@model = Model.find(1)
@child = @model.children.find_by_name(name)
Run Code Online (Sandbox Code Playgroud)
您如何建议将此逻辑移至rails 3?有关另一个存根/模拟库的建议吗?
小智 10
通常你会在控制器规格内模拟模型:
Model.stub!(:find).and_return(mock_model('Model'))
Child.stub!(:find).and_return(mock_model('Child'))
Run Code Online (Sandbox Code Playgroud)
但是,当你进入gem "rspec-rails", "~> 2.0"rails 3应用程序的Gemfile时,标准的rails脚手架生成器将使用rspec为你生成规范,因此运行rails generate scaffold 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
Run Code Online (Sandbox Code Playgroud)