ruby-on-rails – Rspec:控制器中的Stub方法

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rspec:控制器中的Stub方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以知道如何在控制器创建方法中使用存根方法吗?我需要为此编写规范,但是我遇到了这些错误.我需要在模型中创建新的公司记录之前检查控制器中的create方法必须执行validate_fbid方法.

错误

1) Companies new company create with valid information#validate_fbid should have correct parameters and return value
 Failure/Error: CompaniesController.create.should_receive(:validates_fbid).with(company)
 NoMethodError:
   undefined method `create' for CompaniesController:Class
 # ./spec/requests/companies_spec.rb:38:in `block (5 levels) in <top (required)>'

  2) Companies new company create with valid information#validate_fbid should fbid validation passed
 Failure/Error: CompaniesController.create.stub(:validates_fbid).and_return('companyid')
 NoMethodError:
   undefined method `create' for CompaniesController:Class
 # ./spec/requests/companies_spec.rb:43:in `block (5 levels) in <top (required)>'

CompaniesController

def create 
company = Company.new(params[:company])
verifyfbid = validate_fbid(company)

if verifyfbid != false
    if company.fbid.downcase == verifyfbid.downcase
        if company.save 
            @message = "New company created."
            redirect_to root_path
        else 
            @message = "Company create attempt Failed. Please try again."
            render 'new' 
        end 
    else 
        @message = "Company create attempt Failed. Invalid facebook id."
        render 'new' 
    end
else  
    @message = "Company create attempt Failed. No such facebook id."
    render 'new'            
    end             
 end 

  private  
  def validate_fbid(company)
   uri = URI("http://graph.facebook.com/" + company.fbid)
   data = Net::HTTP.get(uri)
   username = JSON.parse(data)['username']      
   if username.nil?
    return false 
   else
    "#{username}"
   end
 end

请求/ companies_spec.rb

context "#validate_fbid" do               
        #validate fbid
        let(:company){ Company.new(name:'Example Company',url: 'www.company.com',fbid: 'companyid',desc: 'Company desc' )}

        it "should have correct parameters and return value" do
            CompaniesController.create.should_receive(:validates_fbid).with(company)
                                .and_return('companyid')
        end

        it "should fbid validation passed" do               
            CompaniesController.create.stub(:validates_fbid).and_return('companyid')
            company.fbid.should_not be_nil
            company.fbid.should == 'companyid'
            company.save
            expect { click_button submit }.to change(Company,:count).by(1)
        end                                             
    end

解决方法

当它是测试用例的主题时,您不希望存根该方法
context "#validate_fbid" do
  #test the function here
  #don't stub
end

当您在控制器中测试创建操作时,您可以存根“validate_fbid”

describe "post create" do
   ...
   CompaniesController.any_instance.stub(:validates_fbid).and_return('companyid')
   ...
end

希望能帮助到你.

原文链接:https://www.f2er.com/ruby/271325.html

猜你在找的Ruby相关文章