Rspec FactoryGirl ::预期失败/错误:收到1次:0次

时间:2013-06-20 04:20:52

标签: ruby-on-rails ruby web rspec record

嗨,我是铁杆上的红宝石。

目前我在使用rspec和FactoryGirl测试轨道上的控制器时遇到问题。我正在尝试测试activerecord以对应我的动作'show',但它没有通过或接收任何。有什么办法可以解决这个问题吗?谢谢

我得到的错误是:

 *Failure/Error: Employment::LevelAccess.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
   (<Employment::LevelAccess(id: integer, employment_id: integer, accessiblity_id: integer, actions: string, user_id: integer, created_at: datetime, updated_at: datetime) (class)>).find("1")
       expected: 1 time
       received: 0 times*

这是我的代码

level_access_controller.erb

context'需要实例'do

  before do
    @level_access = FactoryGirl.create(:level_access_2, employment_id: @employment.id)
  end

  describe 'Get id to Show' do

    it "should find the correct value to show" do
      Employment::LevelAccess.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
      get :show, :employment_id => @employment.id, :id => @level_access.id        
    end

  end

  describe 'Get show' do

    before do
      get :show , :employment_id => @employment.id, :id => @level_access.id 
    end

    it "should render a show page" do
      expect(response).to render_template("show")
    end

    it "should not redirect to any page" do
      response.should_not redirect_to("hahahha")
    end

  end        

end

仅供参考:我也已经为就业做了一个实例。

工厂/ level_access.erb

FactoryGirl.define做

factory :level_access, class: Employment::LevelAccess do
    employment_id   {FactoryGirl.create(:employment).id}
    actions             'create,update,new,destroy,show'
end

factory :level_access_2, class: Employment::LevelAccess do
    actions             'show'
end

2 个答案:

答案 0 :(得分:0)

控制器中的

def show
  @employee = Employee.find(params[:id])
end
控制器rspec中的

before do
  @emp = FactoryGirl.create(:employee)
  get :show , id => @emp.id
end

it "find the employee with provided id" do
  assigns[:employee].should == @emp
end

it "should render a show page" do
  response.should render_template('show')
end

it "should not redirect to any page" do
  response.should_not redirect_to("hahahha")
end

'assigns'允许您从规范中的控制器访问实例变量。有关正确编写rspec的更多信息,请查看http://betterspecs.org/

答案 1 :(得分:0)

我终于通过向项目经理寻求帮助

来弄明白了

他引用了

LevelAccessController

类Manage :: Employments :: LevelAccessesController&lt; InheritedResources ::基地   before_filter:authenticate_user!   默认值:resource_class =&gt;就业:: LevelAccess,:collection_name =&gt; &#39; level_accesses&#39;,:instance_name =&gt; &#39; level_access&#39;

保护

  def begin_of_association_chain
        employment
  end

  def employment
    @employment ||= Employment.find(params[:employment_id])
  end


所以我改变了rspec  描述&#39;获取ID以显示&#39;做

it "should find the correct value to show" do
  Employment::LevelAccess.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
  get :show, :employment_id => @employment.id, :id => @level_access.id        
end

<强>到

        it "should find the correct value to update" do
          @employment.level_accesses.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
          put :update, :employment_id => @employment.id, :id => @level_access.id
        end

因为控制器中的begin_of association_chain关系应该使实例可用于rspec中的关系链接。