简体   繁体   English

rspec ruby​​ on rails关于控制器的新手问题

[英]rspec ruby on rails newbie question about controllers

im new to RoR or rspec and trying to learn it.First of all i cant find tutorials for newbies, just advanced ones, becouse i want to learn RoR and at the same time learn to test. 我是RoR或rspec的新手并尝试学习它。首先,我找不到适合新手的教程,只是高级教程,因为我想学习RoR并同时学习测试。 Im writing test for simple CRUD controllers, but i have no idea how to test for example if params passed to my create action, also if it was saved. 我正在为简单的CRUD控制器编写测试,但是我不知道如何测试例如参数是否传递给我的create动作,以及是否已保存。

def create
    @item = TextItem.new(params[:item])

    if @item.save
       redirect_to(:action => 'index')
    else
       render('new')
      end
  end

I think the better way is to become familiar with rails and ruby and only then dive into tdd/bdd with test/unit or minitest or rspec, cucumber or a lot of other testing techniques. 我认为更好的方法是熟悉rails和ruby,然后再使用测试/单元或minitest或rspec,黄瓜或许多其他测试技术深入到tdd / bdd。

I recommend you to buy a book/ebook to start, for example this one Agile rails development with rails , ebook only $24. 我建议您购买一本书/电子书开始,例如,这个带有rails的敏捷Rails开发 ,电子书仅需$ 24。 You will save a lot of time. 您将节省大量时间。

Sometimes Rspec is a little bit tricky/magical and without base knowledge of rails/ruby it can be difficult to start using. 有时,Rspec有点棘手/不可思议,并且如果没有关于rails / ruby​​的基础知识,可能很难开始使用。

I don't suggest you to study rails and rspec in one attempt. 我不建议您尝试学习Rails和rspec。

BTW specs for this action can be like that: 动作的BTW规范可以是这样的:

  describe "POST create" do
    let(:item) { mock_model(TextItem) }

    before(:each) do
      TextItem.stub(:new).and_return(item)
    end

    it "saves the item" do
      item.should_receive(:save)
      post :create
    end

    context "when the item saves successfully" do
      before(:each) do
        item.stub(:save).and_return(true)
      end

      it "redirects to the items index" do
        post :create
        response.should redirect_to(:action => "index")
      end
    end

    context "when the item fails to save" do
      before(:each) do
        item.stub(:save).and_return(false)
        item.stub_chain(:errors, :empty?).and_return(false)
      end

      it "renders the new template" do
        post :create
        response.should render_template(:new)
      end
    end
  end

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

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