0

嗨,我是 ruby​​ on rails 的新手。

目前我在使用 rspec 和 FactoryGirl 在 Rails 上测试控制器时遇到问题。我正在尝试测试 activerecord 是否与我的动作“显示”相对应,但它没有通过或收到任何内容。我能做些什么来解决这个问题吗?谢谢你

我得到的错误是:

 *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

上下文'需要实例'做

  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

结尾

4

2 回答 2

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/

于 2013-06-20T04:37:28.697 回答
0

我终于通过向我的项目经理寻求帮助来解决这个问题

他引用了

级别访问控制器

类 Manage::Employments::LevelAccessesController < InheritedResources::Base before_filter :authenticate_user!默认值 :resource_class => 就业::LevelAccess, :collection_name => 'level_accesses', :instance_name => 'level_access'

受保护

  def begin_of_association_chain
        employment
  end

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

结尾


所以我改变了描述'Get id to Show'的rspec

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 中的关系链接。

于 2013-06-24T04:31:45.113 回答