0

Using Rspec 3.4, I've got a Message class:

class Message
  include ActiveModel::Model 
end

The following controller spec fails:

context "with invalid params" do
  it "assigns a newly created but unsaved message as @message" do
    post :create, { message: @invalid_attributes }
    expect(assigns(:message)).to be_a_new(Message)

The failure message is:

 Failure/Error: expect(assigns(:message)).to be_a_new(Message)
 NoMethodError:
   undefined method `new_record?' for #<Message:0x00000006dc8a88>

So it looks like rspec calls new_record? when I call be_a_new(Message), and since my Message class does not inherit from ActiveRecord I'm getting this error.

How can I test that assigns(:message) is indeed a new instance of my Message class?

I've tried the following:

# this works, but I don't like it
expect(assigns(:message).to_not be(nil) 

# these fail because the instances are not equal
expect(assigns(:message).to eq(Message.new)
expect(assigns(:message).to eql(Message.new)
expect(assigns(:message).to eqeul(Message.new)
expect(assigns(:message).to be(Message.new)

Is then a railsy way of doing this?

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189

1 Answers1

1

I think you should try following:

expect( assigns(:message) ).to be_kind_of Message
sadaf2605
  • 7,332
  • 8
  • 60
  • 103