How to merge matchers in rspec? - ruby

This is my specs:
it "should convert doc successfully" do
#response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.doc"))
#response[:status].should == 'ok'
File.exist?(#response[:pdf_path]).should be_true
File.exist?(#response[:swf_path]).should be_true
File.exist?(#response[:cover_path]).should be_true
end
it "should convert ppt successfully" do
#response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.ppt"))
#response[:status].should == 'ok'
File.exist?(#response[:pdf_path]).should be_true
File.exist?(#response[:swf_path]).should be_true
File.exist?(#response[:cover_path]).should be_true
end
it "should convert xls successfully" do
#response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls"))
#response[:status].should == 'ok'
File.exist?(#response[:pdf_path]).should be_true
File.exist?(#response[:swf_path]).should be_true
File.exist?(#response[:cover_path]).should be_true
end
How to merge repetition ? thanks

You could declare a custom matcher in a new conversion_helpers.rb file:
RSpec::Matchers.define :be_converted_successfully do
match do |conversion_response|
conversion_response[:status] == 'ok' && File.exist?(conversion_response[:pdf_path]) && File.exist?(conversion_response[:swf_path]) && File.exist?(conversion_response[:cover_path])
end
end
Then in your spec, require 'conversion_helpers' and you can do:
it "should convert doc successfully" do
SharpOffice::Office.process(File.expand_path("spec/fixture/test.doc")).should be_converted_successfully
end
it "should convert ppt successfully" do
SharpOffice::Office.process(File.expand_path("spec/fixture/test.ppt")).should be_converted_successfully
end
it "should convert xls successfully" do
SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls")).should be_converted_successfully
end
Although, in actual testing this could get quite annoying trying to track down a bug. But that's a different issue.

make it as a function?
put the function description in the describe block
def convert_expectation(resp)
resp[:status].should == 'ok'
File.exist?(resp[:pdf_path]).should be_true
File.exist?(resp[:swf_path]).should be_true
File.exist?(resp[:cover_path]).should be_true
end
it "should bla blabla" do
resp = SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls"))
convert_expectation(resp)
end

Related

Rspec to have(n).items undefined method

I'm trying to follow a guide on code.tuts and I keep getting an error.
Here is my Library spec:
require 'spec_helper'
describe Library do
before :all do
lib_arr = [
Book.new("JavaScript: The Good Parts", "Douglas Crockford", :development),
Book.new("Dont Make me Think", "Steve Krug", :usability),
]
File.open "books.yml", "w" do |f|
f.write YAML::dump lib_arr
end
end
before :each do
#lib = Library.new "books.yml"
end
describe "#new" do
context "with no parameters" do
it "has no book" do
lib = Library.new
expect(lib).to have(0).books
end
end
context "with a yaml file name parameters" do
it "has two books" do
expect(#lib).to_have(0).books
end
end
end
it "returns all the books in a given category" do
expect(#lib.get_books_in_category(:development).length).to eql 1
end
it "accepts new books" do
#lib.add_book(Book.new("Designing for the Web", "Mark Boulton", :design))
expect(#lib.get_book("Designing for the Web")).to be_an_instance_of Book
end
it "saves the library" do
books = #lib.books.map { |book| book.title}
#lib.save
lib2 = Library.new 'books.yml'
books2 = lib2.books.map { |book| book.title }
expect(books).to eql books2
end
end
I'm getting that have is undefined. I've figured out it's my lines
expect(#lib).to have(0).books
expect(lib).to have(0).books
Is my syntax out of date? I've googled and I can't find it.
The have/have_exactly, have_at_least and have_at_most matchers were removed from RSpec 3. They're now in the separate rspec-collection_matchers gem.
Or, as zishe says, instead of installing the gem, you can just use eq instead of have/have_exactly, and be >= instead of have_at_least and be <= instead of have_at_most.
Source: http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3

Rspec and prevent tests from being run under some circumstances

I use rspec like this:
describe
it 'should check if the xx':
end
How do I prevent some tests in the it end body from being run if some condition is met? For example, if the function is_disabled returns true then the following tests should not run:
it 'should check if the xx1':
end
it 'should check if the xx2':
end
but the following should:
it 'should check if the xx3':
end
it 'should check if the xx4':
end
can you do :
context "if api calls enabled for MC, #app.is_disabled => 'USD' do
it 'should check if the xx3':
end
it 'should check if the xx4':
end
end
Yes, you can use rspec implicit filters. Example:
describe "if the app is enabled", :unless => #app.is_disabled do
it 'should check if the xx3':
end
it 'should check if the xx4':
end
end
describe "if the app is disabled", :if => #app.is_disabled do
it 'should check if the xx1':
end
it 'should check if the xx2':
end
end

rspec check 'with' argument is true

I have the following rspec example:
describe "with spike" do
it "succeeds" do
a = double('whatever')
a.should_receive(:b).with(true)
a.b('not false')
end
end
How can I make with accept any non-false argument?
Just write an arbitrary message handler:
describe "with spike" do
it "succeeds" do
a = double('whatever')
a.should_receive(:b) { |x|
x.should_not be_false
}
a.b('not false')
end
end

How do I write functional test for this

I am pretty new to rspec. How do I write functional test for following piece of code.
class FooController < ApplicationController
 def new
#title = "Log in to Mint"
#msg = session[:msg]
session[:msg] = nil
end
end
 
How about something like this:
describe FooController do
describe "GET new" do
it "assigns 'Log in to Mint' to #title" do
get :new
assigns(:title).should == "Log in to Mint"
end
it "assigns message session to #msg" do
session[:msg] = "a message"
get :new
assigns(:msg).should == "a message"
end
it "sets message session to nil" do
get :new
session[:msg].should be_nil
end
end
end
See also: Rspec: testing assignment of instance variable

Simple Rspec test fails - for what reason?

Coding one of my first rspec tests. headers == nil prints true, but the next test line headers should be_nil fails. Why?
require 'net/http'
$url_arr = []
$url_arr << ...
$url_arr << ...
$url_arr << ...
module NetHelpers
def get_headers(uri)
Net::HTTP.get_response(URI.parse(uri)).get_fields('Set-Cookie')
end
end
describe "new script" do
include NetHelpers
$url_arr.each do |uri|
it "should not return cookies" do
headers = get_headers(uri)
p "==========> #{headers == nil}"
headers should be_nil
end
end
end
Also, the output is
got: "new script" (using ==)
Why "new script" is printed, while headers really contains nil?
Try
headers.should be_nil
instead.

Resources