Reputation: 15
I am using RSpec to test my nested controllers. I've signed in current_user (member in my case) and it is returning a different instance. Please see below:
Spec snippet:
before :each do
@request.env["devise.mapping"] = Devise.mappings[:member]
@member = create(:member)
sign_in @member
@calendar = @member.calendars.create!({"name" => "test"}, Calendars::Native)
end
it "creates a new Event" do
puts "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
puts @member
puts @calendar
puts @calendar.events.count
puts "->->->->->->->->->->->->->->->->->->->->->->->->->->->-"
post :create, {:native_id => @calendar.id, :event => valid_attributes}, valid_session
puts "<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<"
puts @member
puts @calendar
puts @calendar.events.count
puts "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
expect {
post :create, {:native_id => @calendar.to_param, :event => valid_attributes}, valid_session
}.to change(@calendar.events, :count).by(1) #Event, :count).by(1)
end
Controller Snippet
def create
set_calendar
@event = @calendar.events.build(event_params)
respond_to do |format|
if @event.save
puts "DID SAVE"
puts @member
puts @calendar
puts @event
format.html { redirect_to native_event_url(:native_id => @calendar.id, :id => @event.id), notice: 'Event was successfully created.' }
format.json { render action: 'show', status: :created, location: @event }
else
format.html { render action: 'new' }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def set_calendar
@member = current_member
@calendar = @member.calendars.find(params[:native_id])
end
Console output:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#<Member:0x007fb5a6e4fda0>
#<Calendars::Native:0x007fb5a495bf58>
0
->->->->->->->->->->->->->->->->->->->->->->->->->->->-
DID SAVE
#<Member:0x007fb5a49a1aa8>
#<Calendars::Native:0x007fb5a49a0810>
#<Event:0x007fb5a49b3eb0>
<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<
#<Member:0x007fb5a6e4fda0>
#<Calendars::Native:0x007fb5a495bf58>
0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see... Member objectId is different, which results in Rspec failing test because count doesn't change.
Any good suggestion? What's going on?
Thanks!!
Upvotes: 0
Views: 456
Reputation: 11647
This is fine. You're just getting a bit confused.
When you first create @member
, it gets stored in memory. It has object ID X. Then, in your actual controller, data is grabbed from the current_user
method, and even though it's the same underlying data as @member
, it's actually a different object in memory, so it has object ID Y. Your test is holding on to one object, your code on to another.
Instead of doing puts @member
, try doing puts @member.inspect
, or as a short cut, p @member
. This will show you more information. You will probably get output like this instead:
XXXXXXXXXXX
#<Member id: 1, :name "John Doe">
->->->->->->
DID SAVE
#<Member id: 1, :name "John Doe">
<-<-<-<-<-<-
#<Member id: 1, :name "John Doe">
XXXXXXXXXXXX
You'll see that the information about the user is the same. The fact that they are not the exact same object instance in memory doesn't matter.
Upvotes: 1