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?
我认为您应该尝试以下操作:
expect( assigns(:message) ).to be_kind_of Message
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.