Skip to content

Instantly share code, notes, and snippets.

@BenEddy
Last active December 21, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenEddy/6348313 to your computer and use it in GitHub Desktop.
Save BenEddy/6348313 to your computer and use it in GitHub Desktop.
describe UserSearch do
describe "#results" do
let(:user_scope) { double }
before do
User.stub(scoped: user_scope)
end
def results(options = {})
described_class.new(options).results
end
it "returns Users" do
expect(results).to eq(user_scope)
end
it "scopes by name" do
scoped_by_name = double
user_scope.stub(:name_matching).with("Lee")
.and_return(scoped_by_name)
# Note that 'results' is a method defined above
expect(results(name: "Lee")).to eq(scoped_by_name)
end
it "scopes by email" do
scoped_by_email = double
user_scope.stub(:email_matching)
.with("lee@gmail.com")
.and_return(scoped_by_email)
expect(results(email: "lee@gmail.com")).to eq(scoped_by_email)
end
it "combines options" do
scoped_by_name_and_email = double
scoped_by_name = double
user_scope.stub(:name_matching)
.with("Lee")
.and_return(scoped_by_name)
scoped_by_name.stub(:email_matching)
.with("lee@gmail.com")
.and_return(scoped_by_name_and_email)
expect(results(name: "Lee", email: "lee@gmail.com"))
.to eq(scoped_by_name_and_email)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment