File: fake_ar_attributes_spec.rb

package info (click to toggle)
ruby-bogus 0.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 828 kB
  • ctags: 628
  • sloc: ruby: 4,124; makefile: 6; sh: 2
file content (72 lines) | stat: -rw-r--r-- 1,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'spec_helper'
require 'active_record'
require 'nulldb'

describe "Stubbing ActiveRecord::Base subclasses" do
  ActiveRecord::Schema.verbose = false
  ActiveRecord::Base.establish_connection :adapter => :nulldb

  ActiveRecord::Schema.define do
    create_table :blog_posts do |t|
      t.string :name
      t.string :author
    end
  end

  class BlogPost < ActiveRecord::Base
    def author(x)
      "#{x} #{self[:author]}!"
    end
  end

  include Bogus::MockingDSL

  before do
    Bogus.configure do |c|
      c.fake_ar_attributes = true
    end
  end

  it "makes it possible to stub active record fields" do
    post = fake(:blog_post, name: "hello")

    expect(post.name).to eq "hello"
  end

  it "works only when enabled in configuration" do
    Bogus.configure do |c|
      c.fake_ar_attributes = false
    end

    expect {
      fake(:blog_post, name: "hello")
    }.to raise_error(NameError)
  end

  it "does not overwrite existing method signatures" do
    post = fake(:blog_post)

    post.author("hello")

    expect(post).to have_received.author("hello")
  end

  class ExampleForActiveRecordAttributes
    def foo(x)
    end
  end

  it "does not interfere with non-ar classes" do
    fake = fake(:example_for_active_record_attributes)

    fake.foo(1)

    expect(fake).to have_received.foo(1)
  end

  after do
    Bogus.configure do |c|
      c.fake_ar_attributes = false
    end
  end
end