Print the Page content in Capybara

Capybara is arguably one of the most used tools in testing in Ruby on Rails. If you need to show the content of the page you are testing, you can use the following command

page.body

This is helpful if you want to see what the problem is with your tests.

There is another really helpful command that you can use.

save_and_open_page
=> #<Process::Waiter:0x007fe07aecbbe0 sleep>

This will open a web browser page and show the content. You wouldn’t see any design here.

Screen shot 2017 04 26 at 8.45.51 pm

 Setting Rails Environment Variables

It is always a better idea for configuration variables to stay on the server as environment variables instead of putting them on your repository. It makes it less likely for someone to have access to them. Here are a few ways to set environment variables for your Rails application.

~/.bashrc File

This is the simplest and most obvious of the 3. It is default way you are going to do it. Just add the environment variables in your ~/.bashrc file.

export APPLICATION="Application Name"
export APPLICATION_VERSION="Application Version"

dotenv

dotenv is a Ruby gem to load environment variables from an env file.

Installing

gem 'dotenv-rails', groups: [:development, :test]

And then execute:

$ bundle

figaro

Figaro is a nice gem that simplifies configuration files.

gem "figaro"

You might need to add the additional yml file to .gitignore.

# config/application.yml

application_conf: "882354"
application_key: "fausdhf7(0sadfsmd__nfasdf"
some_conf: "23456789hdsfdsfasdf132"

These can be used throughout the application via ENV["application_key"]

 React.Component cannot use getInitialState

When React 0.13 was launched, you no longer needed to use React.createClass.

These are basically the same.

class YourComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { /* initial state */ };
  }
}
var YourComponent = React.createClass({
  getInitialState() {
    return { /* initial state */ };
  },
});

getInitialState won’t work when you are using React.Component. Using it will show up as an error in your console:

Warning: getInitialState was defined on YourComponent, a plain JavaScript class. This is only supported for classes created using React.createClass. Did you mean to define a state property instead?

Btw, The React community is moving closer to ES6. Also it is considered as the best practice.

 Your password has expired. To log in you must change it using a client that supports expired passwords

Started GET "/" for 127.0.0.1 at 2017-03-14 05:29:35 +0800

Mysql2::Error (Your password has expired. To log in you must change it using a client that supports expired passwords.):

This is an instance I encountered a few days ago. Apparently, MYSQL allows users to automatically expire passwords.

To fix the issue, log in to your MySQL console using your old password.

You will need to alter your password by using the following command:

ALTER USER `root`@`localhost` IDENTIFIED BY 'new_password', `root`@`localhost` PASSWORD EXPIRE NEVER;

Here it is in action!

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.10

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use database your_database
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> ALTER USER `root`@`localhost` IDENTIFIED BY 'new_password', `root`@`localhost` PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye

Voila! done!