Re: [rspec] What should be the behaviour of a let variable when passed in as an argument to a custom matcher

12 views
Skip to first unread message

David Chelimsky

unread,
Dec 22, 2012, 7:44:40 AM12/22/12
to rs...@googlegroups.com
On Sat, Dec 22, 2012 at 1:52 AM, Chris Gat <chri...@gmail.com> wrote:
> I have something similar to the following code
>
> feature Something do
> let!(:var) { FactoryGirl.create(:var) }
> include_examples "these examples"
> end
>
> shared_examples_for "these examples" do
> scenario "test" do
> visit some_url
> expect(page).to have_values(var)
> end
> end
>
> RSpec::Matchers.define :have_values do |expected|
> match do |actual|
> page.has_text(expected.name)
> end
> end
>
> FactoryGirl.define do
> factory :var do
> sequence(name) {|n| "name#{n}}
> end
> end
>
> The problem I'm having is that let doesn't seem to memoize var into the
> matcher. Since var.name is a sequence, the name on the page when it is
> visited is different from the check in the customer matcher.
>
> Rspec 2.12, Capybara 2
>
> Thanks in advance for the help

What you've posted should work, assuming that some_url points to a
page that lists all of the var objects' names.

The fact that you're using a sequence shouldn't matter at all. Using
let!() (vs just let()) means that the var object is generated before
the example (scenario in this case), and any call to "var" returns
that same object.

Any chance you could post the exact code (including the controller
action, failure message, and possibly output of the actual html) so we
can see what's really going on? It's quite common for things to get
lost in translation when you write "I have code like this" instead of
the actual code.

Cheers,
David

Chris Gat

unread,
Dec 26, 2012, 3:56:31 PM12/26/12
to rs...@googlegroups.com
Hi David,

Thanks for the reply and sorry for the late response (the holidays got in the way). After reading your reply I went back and double checked my code (before dumping on here) and found out what I was doing wrong. Without going into too much detail, I had pluralized a hash key when it shouldn't have been, which resulted in the test passing. When I tried to debug the custom matcher for the test, I was actually debugging the custom matchers use in another example, which caused my confusion.

Again, thanks for the response.

Merry Christmas,

Chris
Reply all
Reply to author
Forward
0 new messages