Carpela
Carpela

Reputation: 2195

Using result of raise_error (capybara)

I'm using capybara to do some web automation. There are various points in the code where it says things like

raise_error 'Failed as this is a duplicate' if duplicate?

or

raise_error 'Failed to log in' if logged_in? == false

All of this is abstracted to a module and I'd prefer that module to not rely on anything in the models.

What I'm struggling with is how to access that error text when I'm running it, from outside the model.

i.e.

Class Thing

  has_many :notes
  def do_something

     @done = Module::Task.something(self.attribute)

     if @done
       self.update_attributes(status:'Done')
     else
       self.notes.new(text: error.text)
     end

  end

but I can't work out the syntax to get that error text.

Upvotes: 0

Views: 539

Answers (1)

W4rQCC1yGh
W4rQCC1yGh

Reputation: 2219

Answer: If I understand you correctly then errors that appeared while completing the task

@done = Module::Task.something(self.attribute)

can be accessed via @done.errors.messages

Example: If I have User model where attribute username has 2 validations: presence and format then error messages display like this:

irb(main):019:0* u = User.new
irb(main):022:0* u.save # wont succeed
irb(main):028:0* u.errors.messages
  => {:uid=>["can't be blank", "is invalid"]}

If you want to test error messages with capybara then you can use the syntax like this:

it 'raises jibberishh' do
  expect{User.raise_error_method}.to raise_error("jibberishh")
end

Upvotes: 1

Related Questions