In controller you can pass create by the code below:
expect {
post :create, params: {question: valid_attributes}, session: valid_session
}.to change(Question, :count).by(1)
What if the create is inside of a method? I have a method that behaves find or create. And it will return the id.
def my_method(id='')
if id.blank?
question = Question.create(content: "The content.")
else
question = Question.find(id)
end
question.id
end
I do the Rspec like this:
it "should create for non existence of id" do
expect(my_method).to have(1).record
end
it "should return object base on id" do
expect(assigns(:question)).to eq(question)
end
But it gives me this error:
NoMethodError:
undefined method `have' for #<RSpec::ExampleGroups::ClassNameHere:0x00000008543a50>
Can someone help? Thanks!
my_methodplaced? and where do you call it from?