Skip to content

Commit

Permalink
add tests for these methods: to_s,to_json, and self.from_json
Browse files Browse the repository at this point in the history
  • Loading branch information
Benawi committed Aug 25, 2023
1 parent 86bc3ce commit fe930cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../lib/classes/book'
require 'rspec'
require 'json'
describe Book do
before(:each) do
@book8 = Book.new('2013-02-12', 'Orbit', 'Good', archived: false)
Expand Down
29 changes: 29 additions & 0 deletions spec/genre_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,33 @@
expect(new_genre.items.length).to eq 1
end
end

describe "#to_json" do
it "converts to json" do
cart = Genre.new("My Cart")
cart.items << Item.new("Item 1")

expect(cart.to_json).to eq({
name: "My Cart",
items: [cart.items[0].to_json]
}.to_json)
end
end

describe "#to_s" do
it "returns string with id and name" do
cart = Genre.new("My Cart")

expect(cart.to_s).to eq("Id: #{cart.id}, Name: My Cart")
end
end

describe ".from_json" do
it "creates a Cart from json" do
json = {name: "My Cart"}.to_json
cart = Genre.from_json(JSON.parse(json))

expect(cart.name).to eq("My Cart")
end
end
end

0 comments on commit fe930cb

Please sign in to comment.