0

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!

1
  • Where is this my_method placed? and where do you call it from? Commented Nov 23, 2016 at 5:41

1 Answer 1

1

There is no magic. Instead of:

expect(my_method).to have(1).record

you should explicitly call the method (I assume it’s a class method of Question) and then check Question for having records. Methods do not have records for obvious reason:

Question.my_method
expect(Question).to have(1).record
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.