
Dir.glob('weblogic/adomain/*') do |file|
next unless test(?d, file)
FileUtils.expects(:cp_r).with(file,'new_server/weblogic/adomain')
end
To be honest I didn't want to be setting expectations in a loop. A regex would handle this much better, right? Turns out you can pass a block to the with clause that has a regex in it. That would look something like this:
FileUtils.expects(:cp_r).with() { |value| /new_server\/weblogic\/adomain/ === value}
That isn't too bad, but I think this is nicer:
FileUtils.expects(:cp_r).with(/new_server\/weblogic\/adomain/)
Unfortunately Mocha, the mocking framework I am using here doesn't let you. Sooo, I submitted a patch. I'm really hoping this makes it in as I think it is a handy thing. As a side note, it turns out there was already a similar patch against Mocha for this submitted by Wolfgang Schnerring. It had been ignored for about 7 months! I incorporated the best of both into one. Let's hope this one gets looked at sooner.
4 comments:
I think the regexp matching should have something to make it more intentional. If you see .with(/foo/) how do you know if you're literally expecting a regex of foo or if you're expecting an arg that matches /foo/?
Maybe .with(matching(/foo/)) ? Kind of like using the 'anything' method to match args in Mocha.
daniel,
That is a good point, and it gives me a failing test for my patch too! Should have thought of that. Thanks.
As for the suggestion, I hate to give up the brevity of just throwing a regex directly in. And most of the time the ambiguity won't be a problem.
What do you think about making it work both ways? Just throwing a regex in the expectation will match (===) or (==). Using matching() will match only (===) and we can throw in an equals() that will match only (==).
Curious to hear your opinion.
So James Mead, the fellow behind mocha, has added exactly what daniel has suggested here. Which is totally great!!!!
Check it out!
http://mocha.rubyforge.org/classes/Mocha/ParameterMatchers.html#M
Cool, I'm glad they added that to Mocha.
Post a Comment