2015-09-17 9 views
5

के भीतर से नियंत्रक आवृत्ति चर का उपयोग करना क्या मुझे अपने आरएसईटी परीक्षणों के भीतर से नियंत्रक कार्रवाई में बनाए गए इंस्टेंस वेरिएबल्स को देखने में सक्षम नहीं होना चाहिए? -एक rspec नियंत्रक spec

# /app/controllers/widget_controller.rb 
... 
def show 
    @widget = ... 
    puts "in controller: #{@widget}" 
end 
... 

# /spec/controllers/widget_controller_spec.rb 
RSpec.describe WidgetController, type: :controller do 
... 
describe "GET #show" do 
    it "assigns the requested widget as @widget" do 
    get :show, { :id => 1 } # this is just an example - I'm not hardcoding the id 

    puts "in spec: #{@widget}" 
    end 
end 
... 

यहाँ जब मुझे लगता है कि कल्पना चलाने के उत्पादन मैं मिलता है:

controller: #<Widget:0x007f9d02aff090> 
in spec: 

मैं सोच रहा था कि मैं में @widget तक पहुंच होनी चाहिए में गलत हूँ मेरी नियंत्रक कल्पना?

उत्तर

11

assigns विधि का उपयोग करें:

it "assigns the @widget" 
    expect(assigns(:widget)).not_to be_nil 
end 
बेशक

, आप widget निरीक्षण कर सकते हैं के रूप में आप चाहते हैं, लेकिन यह देखना कि @widget अपने प्रदान की नियंत्रक कोड से, मैं सिर्फ चेक किया गया है बिना अगर यह nil

था

आप puts के लिए इच्छुक रहे हैं विजेट, अपने उदाहरण की तरह, बस इसे assigns

puts assigns(:widget) 
के साथ प्रिंट
संबंधित मुद्दे