2010-09-28 22 views
14

इम एक शर्त है जहाँ सफल साइनअप पर एक सफलता खाका निम्नलिखित नियंत्रक कोडRspec परीक्षण टेम्पलेट्स प्रदान की जा रही


def create 
    @user = User.new(params[:user]) 
    if @user.save 
     render :template => "success" 
    else 
     flash[:notice] = "Oops Somethings not quite right! :(" 
     render :action => "new" 
    end 
    end 
 

मैं इस कोड


before(:each) do 
    @user = User.new 
    @user.attributes = valid_attributes  
    @params = valid_attributes 
    @user.stub!(:save).and_return(true) 
    end 


    def do_post 
    post :create 
    end 


    it "should create new user " do 
    count = User.count 
    do_post 
    user = User.new(@params)  
    user.save.should eql(true) 
    User.count.should eql(count + 1) 

    end 

    it "should render the success page on successful signup" do 
    do_post 
    @user.save 
    response.should render_template("success") if @user.save 
    end 
बाहर का परीक्षण करने के बाद कल्पना का उपयोग कर रहा द्वारा प्रदान की गई है परीक्षण करने के लिए कोशिश कर रहा है

लेकिन उदाहरण के लिए विफल रहता है यह त्रुटि संदेश


1) 
'UsersController handling POST /users should render the success page on successful signup' FAILED 
expected "success", got "users/new.html.erb" 
./spec/controllers/users_controller_spec.rb:67: 
 
के साथ "यह सफल साइनअप पर सफलता पेज प्रस्तुत करना चाहिए"

सफलता दृश्य दृश्य/उपयोगकर्ताओं/बिना किसी कार्रवाई के संग्रहीत टेम्पलेट है। मैं अनुमान लगा रहा हूं कि मैं एक बहुत ही मौलिक गलती कर रहा हूं और कुछ मदद चाहूंगा।

+0

मैं हटा आपके अंतिम पर user.save स्थिति अभिकथन। – Rimian

उत्तर

24

आप परीक्षण में @user चर डाल रहे हैं, लेकिन नियंत्रक एक नया उदाहरण तत्काल कर देगा ताकि स्टब जगह में न हो।

इस मामले में एक सफल बचत कॉल का अनुकरण करने के लिए इस मामले में स्टब का उपयोग करना अच्छा नहीं है। आप इसके बजाय वैध डेटा क्यों नहीं देते हैं और सुनिश्चित करते हैं कि कार्रवाई सफल है?

निम्नलिखित कोड आरएसपीसी> 2.1 के लिए है और यह expect वाक्यविन्यास का उपयोग करता है।

before(:each) do 
    @params = valid_attributes 
end 

it "should create new user" do 
    @_before = User.count 
    post :create, :user => @params 

    expect(assigns(:user)).to_not be_new_record 
    expect(User.count).to eq(@_before + 1) 
end 

it "should render the success page on successful signup" do 
    post :create, :user => @params 

    expect(response).to be_successful 
    expect(response).to render_template("success") 
end 

अंत में, परिवर्तन

render :template => "success" 

render :action => "success" 

पिछले RSpec संस्करणों के लिए या यदि आप should सिंटैक्स का उपयोग करने के लिए, का उपयोग

before(:each) do 
    @params = valid_attributes 
end 

it "should create new user" do 
    @_before = User.count 
    post :create, :user => @params 

    assigns(:user).should_not be_new_record 
    User.count.should == (@_before + 1) 
end 

it "should render the success page on successful signup" do 
    post :create, :user => @params 

    response.should be_successful 
    response.should render_template("success") 
end 
+1

वह था। मुझे अभी पता चला। धन्यवाद। मैं अभी भी रुपेक के साथ काम करने की समझ में प्रक्रिया में हूं। आपका बहुत बहुत धन्यवाद। – Sid

संबंधित मुद्दे