Week 2 Questions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How would you generate a mailer and it’s email templates?

A

rails generate mailer UserMailer account_activation password_reset

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When you generate a mailer in the command line it creates two views for the email template. Why does this happend

A

One for plain-text email and one for HTML email.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does user:references add in this migration:

rails generate model Micropost content:text user:references

A

Automatically adds a user_id column (along with an index and a foreign key reference)3 for use in the user/micropost association.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you set a default scope for an object to be in descending order

A
#in the model 
default_scope -> { order(created_at: :desc) }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What validation would you write to destroy a post that belongs to a user that has been destroyed

A
#in user model
has_many :microposts, dependent: :destroy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you code a shared errors partial and how would you code the render snippet?

A
<div>
    <div class="alert alert-danger">
      The form contains .
    </div>
    <ul>
  <li>

</ul>

</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain this code:

Micropost.where(“user_id = ?”, id)

A

Find microposts where the user_id is equal to the id of the current user

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you use the pluralize method on microposts belonging to a user

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the request.referrer method do?

A

It goes to the previous URL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In one line, how would you write an image tag that displays a micropost’s image if it has one

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In the ruby language, what are the differences between Array.length, Array.size, and Array.count?

A

Array#length method used on arrays in ruby returns the number of elements in the array

Array#size is just an alias to the Array#length method. It executes same Array#length method internally.

Array#count has some more functionalities than length/size. It can be used for getting number of elements based on some condition.
3 ways to use Array#count:
c = [1,2,3,4,4,7,7,7,9]
1. Array#count Returns number of elements in Array
c.count
=> 9
2. Array#count n Returns number of elements having value n in Array
c.count 7
=> 3
3. Array#count{|i| i.even?} Returns count based on condition invoked on each element array
c.count {|i| i>5}
=> 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you write an if else statement depending on a specific controller action?

A
if params[:action] == "foo"
  # Show stuff for action 'foo'
elsif params[:action] == "bar"
  # Show stuff for action 'bar'
elsif ...
  # etc.
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you apply a layout in the controller?

A
#create a new layout_name.hmtl.erb file under layouts in views
#in the controller
layout 'layout_name'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you view all the rails generator commands in the terminal?

A

rails g

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is required at the top of each spec?

A

require ‘rails_helper’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a simple feature spec for visiting the homepage and seeing a h1 tag with jar of awesome as the text

A
require 'rails_helper'
feature "user visits homepage" do
  scenario "successful visit" do
    visit root_path
    expect(page).to have_css 'h1', text: 'Jar of Awesome'
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How would you write an acceptance test for the creation of a note?

A
require 'rails_helper'
feature "User create note" do
  scenario "successful" do
    visit root_path
    click_on "New Note"
    fill_in "Conent", with: "Ate pad thai with sweet tangerines"
    click_on "Submit"
expect(page).to have_css '.notes', text: 'Ate pad thai with sweet tangerines'   end end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Where do you place Object Pattern

A

spec/support/object_pattern.rb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you include Capybara methods in a Object Pattern file?

A

include Capybara::DSL in the class

class NewNoteForm
include Capybara::DSL
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
When writing methods for Object Pattern, why would you return self in this method?
  def visit_page
    visit('/')
    click_on "New Note"
    self
  end
A

You return self so that you can chain multiple methods to the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What do you have to add to the top of the spec to use and Object Pattern object?

A

require_relative ‘../support/new_note_form’

22
Q

How do you code a let statement for a new_note_form

A

let(:new_note_form){NewNoteForm.new}

23
Q

How would you write a scenario test using Factory Girl that visits the show page of a particular note object that has content ‘Just did it’?

A

scenario ‘note page’ do
note = FactoryGirl.create(:note, content: ‘Just did it’)
visit(‘/notes/#{note.id}’)

expect(page).to have_content('Just did it')   end
24
Q

fix this–>How would you write the enum for a privacy option for a note class in the note.rb file? How would you write the input for a privacy option using simple form?

A

simple form in views

#note.rb
enum privacy: [:public_note, :private_note, :friends_access]
25
Q

Explain this line of code note = FactoryGirl.create(:note, content: ‘Just did it’)

A

variable name note
is equal to
A Factory Girl create factory called :note
In this situation we are over riding the default factory’s value of the content: attribute with ‘Just did it’

26
Q

How would you code a sequence in a note factory for the content attribute?

A

sequence(:content){|n| “Note content #{n}”}

27
Q

How would you create a sub factory for a note factory with private access?

A

factory :public_note do
privacy Note.privacy[:public_access]
end

28
Q

Explain this line of code:

notes = FactoryGirl.create_list(:note, 3)

A

variable notes
equal
FactoryGirl creates list of 3 note factories

29
Q

What does controller do?

A
  1. authenticate and authorize requests
  2. handle models
  3. create response
    a) render template
    b) respond with required format and headers (e.g. JSON)
    c) redirect to another route
30
Q

RSpec.describe NotesController, type: :controller do

end
What parts of this line of code can can be removed/are optional?

A
#new code
describe NotesController do
end
# type: :controller
Because our spec is placed in the controller folder, Rails knows that this spec if for a controller and the line of code is not necessary but still works if you want your spec to be extra specific
31
Q

How do you write the describe description for testing controller actions?

A
decribe "GET new" do
#request type
#name of action
32
Q

What are the 4 things you should write testing for when testing controllers?

A

authentication
authorization
response
data assignment

33
Q

what gem must you add to your gemfile to test controllers?

A

gem ‘rails-controller-testing’

34
Q

How do you test for a get response of the new page?

A

it “renders :new template” do
get :new
expect(response).to render_template(:new)
end

35
Q

Write the rest of this test

it “assigns new Note to @note”

A

it “assigns new Note to @note” do
get :new
expect(assigns(:note)).to be_a_new(Note)
end

#assigns contains all instance variables of the action
#assigns takes a symbol as the instance variable name
#be_a_new takes a class and makes sure that the assigned object is a new measure of this provided class
36
Q

Finish the rest of this test:
let(:note) {FactoryGirl.create(:public_note)}
it “renders :show template” do
end

A

let(:note) {FactoryGirl.create(:public_note)}
it “renders :show template” do
get :show, id: note.id
expect(response).to render_template(:show)
end

37
Q
#The class is Note
#Include a factory for an inherited factory called :public_note
it "redirect_to :show template" do
  end
A

it “redirect_to :show template” do
post :create, note: FactoryGirl.attributes_for(:public_note)
expect(response).to redirect_to(note_path(assigns[:note]))
end

38
Q

What does this code do?

expect {
post :create, note: FactoryGirl.attributes_for(:public_note)
}.to change(Note, :count).by(1)

A

it compares the count of Note before and after the code in expect is run after comparing, the count should change by 1

39
Q

What does the change matcher do?

A

The change matcher is used to specify that a block of code changes some mutable state.
You can specify what will change using either of two forms:

expect { do_something }.to change(object, :attribute)
expect { do_something }.to change { object.attribute }
You can further qualify the change by chaining from and/or to or one of by, by_at_most,
by_at_least.

40
Q

In Capybara, what is the difference between:
click_link(‘Link Text’)
click_button(‘Save’)
click_on(‘Link Text’)

A

click_link(‘Link Text’)# clicks on links
click_button(‘Save’)# clicks on buttons
click_on(‘Link Text’)# clicks on either links or buttons

41
Q

What is assigns(:note) equivalent to while used as an argument in the expect() method?

A

it is equivalent to an instance variable

42
Q
When writing an action for a Page Object Pattern class, what does the params={} in the action fill_in_with  do?
def fill_in_with(params={})
A

This action takes attributes that will be passed into the params of the page

43
Q

How would you provide a defalut value for the following action of a Page Object Pattern class?
def fill_in_with(params = {})
fill_in(‘Title’, with: :title, ‘Read a book’)
fill_in(‘Description’, with: ‘Excellent read’)
select(‘Public’, from: ‘Privacy’)
check(‘Featured achievement’)
attach_file(‘Cover image’, “#{Rails.root}/spec/fixtures/cover_image.png”)

self   end
A

wrap the value in params.fetch(:attribute_symbol, value_to_make_defalut)

example:
fill_in(‘Title’, with: params.fetch(:title, ‘Read a book’params.fetch)

44
Q

What is the definition of:

fetch(key, *args)

A

Returns a parameter for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise an ActionController::ParameterMissing error; if more arguments are given, then that will be returned; if a block is given, then that will be run and its result returned.

params = ActionController::Parameters.new(person: { name: ‘Francesco’ })

params. fetch(:person) # => {“name”=>”Francesco”}
params. fetch(:none) # => ActionController::ParameterMissing: param is missing or the value is empty: none
params. fetch(:none, ‘Francesco’) # => “Francesco”
params. fetch(:none) { ‘Francesco’ } # => “Francesco”

45
Q

When would you use sequences in FactoryGirl

A

If you want to create multiple objects to be tested and you want to by-pass uniqueness validations set in the model

46
Q

How do you write the describe description shorthand for a method and an instance method?

A

describe ‘.authenticate’ do

describe ‘#admin?’ do

47
Q

What is the difference between Class and Instance Methods

A
Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class.
example:
class Foo
  def self.bar
    puts 'class method'
  end

def baz
puts ‘instance method’
end
end

Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class

Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #

48
Q

Name 3 ways to create a class method in ruby

A
# Way 1
class Foo
  def self.bar
    puts 'class method'
  end
end

Foo.bar # “class method”

# Way 2
class Foo
  class << self
    def bar
      puts 'class method'
    end
  end
end

Foo.bar # “class method”

# Way 3
class Foo; end
def Foo.bar
  puts 'class method'
end

Foo.bar # “class method”

49
Q

Name 3 ways to create an instance method in ruby

A
class Foo
  def baz
    puts 'instance method'
  end
end

Foo.new.baz # “instance method”

Way 2
class Foo
attr_accessor :baz
end

foo = Foo.new
foo.baz = ‘instance method’
puts foo.baz

# Way 3
class Foo; end

foo = Foo.new
def foo.bar
puts ‘instance method’
end

Foo.new.baz # “instance method”

50
Q

How long should a spec description be?

A
40 characters
If longer consider using the context method:
BAD
it 'has 422 status code if an unexpected params will be added' do
GOOD
context 'when not valid' do
  it { is_expected.to respond_with 422 }
end
51
Q

What is lazy loading and explain a benefit of it?

A

Your database is queried only when data from the database is required for some kind of manipulation in code.
This design pattern decreases the time required by an application to boot by distributing the computation cost during the execution. Also, if a specific feature is never used, the computation won’t be executed at all.