English 中文(简体)
如何在控制器中发现一个范围更广的新物体
原标题:How to spec a scoped new object in the controller

我应怎样看待这一nes建:

#projects_controller.rb
def new
  @account.projects.build
end

到目前为止,我有这样的情况:

#projects_controller_spec.rb
describe ProectssController do
  describe "GET new" do
    let(:account) { mock_model(Account) }
    let(:project) { mock_model(Project).as_null_object }

    before do
      Account.stub(:find_by_subdomain!).and_return(account)
      #Project.should_receive(:build).with(:account_id => account.id).and_return(project)
    end

    it "assigns @project" do
      get :new
      assigns[:project].should eq(project)
    end
  end
end

不要确定我应如何看待这一点......

最佳回答

在项目总经理中,这项任务没有完成。 应:

def new
  # ...
  @project = @account.projects.build
  # ...
end

那么,你可以 st倒地重复你的意图:

it "assigns @project" do
  account = mock_model(Account) 
  Account.stub(:find_by_subdomain!).and_return(account)
  project = account.stub_chain(:projects,:build) { mock_model(Project) }
  get :new
  assigns(:project).should == project
end
问题回答

总的来说,我建议尽可能少地进行校对和模拟。 我建议使用“工厂女童”等一些东西,为测试创造真正的数据库目标,以便与这些试验互动。 这意味着<代码>Account和项目将是实时查询的班次,然后是@account 真正的AR物体,有<条码>项目协会,与生产一样。 这一点很重要,因为否则,你只是重新测试你撰写的版本,并且实际上在实际使用“积极记录”时测试你的代码的功能。

一旦你能够这样做,我只建议检查你对项目模式的关心,例如:

assigns[:project].should be_instance_of(Project)
assigns[:project].should be_new_record
assigns[:project].account.should == logged_in_user

希望这一帮助!





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签