-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
interactions in given/setup blocks should have precedence over those in setup methods #321
Comments
Reported by |
Reported by |
Reported by |
+1 |
As a workaround while keeping the Behaviour Definition at the right place, I did currently sth like this: class MyInterfaceTest extends Specification {
def myInterface = Mock(MyInterface)
def myMethodReturn
def setup() {
myMethodReturn = 1
myInterface.myMethod() >> {return myMethodReturn}
}
def "testing regular behaviour"() {
when:
def result = myInterface.myMethod()
then:
result == 1
}
def "testing exceptional behaviour"() {
given:
myMethodReturn = 2
when:
def result = myInterface.myMethod()
then:
result == 2
}
} |
+1 |
1 similar comment
+1 |
I think the best way to do this while retaining a) backward compatibility and b) make it optional (sometime I don't want it to be overriden) would be a new syntax/method, something like: def "overriding setup mocks"() {
given:
myMock.redefine {
oldMethod >> "bla"
}
....
} |
That would work great! |
+1 This issue was created in 2011, any update on it? It's still marked "New"... |
+1 any news regarding this? I am selling finally to my new team Spock, and this is one of the things that is confusing for them coming from Junit |
+1 |
If you are using a def "testing exceptional behaviour"() {
given:
myInterface.myMethod() >> 1
when:
def result = myInterface.myMethod()
then:
result == 2
_ * myInterface.myMethod() >> 2
} Only use |
Hi @leonard84 I am confused why this is closed as the behaviour is still the same. |
@huehnerlady it is by design, that the interactions with verifications in the See also this variant. def setup() {
myInterface.myMethod() >> 1
}
def "testing exceptional behaviour"() {
when:
def result = myInterface.myMethod()
then:
result == 2
_ * myInterface.myMethod() >> 2
} |
@leonard84 I believe that the problem is why we cannot specify such precedence in |
@leonard84 imagine the scenario where you define a mock in the setup because you don't want to add it in every single test, but there are few cases where you need to override the expectation in the given of the test, that is what is missing. |
Case 1) List myMock = Mock(List) {
get(0) >> 1
}
def "normal testcase"() {
when:
def result = myMock.get(0)
then:
result == 1
}
def "exception testcase"() {
when:
def result = myMock.get(0)
then:
result == 2
1 * myMock.get(0) >> 2
} works right now. List myMock = Mock(List) {
1 * get(0) >> 1
}
def "normal testcase"() {
when:
def result = myMock.get(0)
then:
result == 1
}
def "exception testcase"() {
when:
def result = myMock.get(0)
then:
result == 2
1 * myMock.get(0) >> 2
} will fail. I closed it because the ticket described case 1) which works correctly. |
@leonard84 I believed that ticket describes 3rd case:
and the question is why it cannot be done this way as putting this "overwrite" behaviour in |
@vanta it might look like a hack to you, but it is not. What you describe is just adding another stub call after the first one. Changing that behavior would break the existing code for many others, and having a valid way to override it I don't see it as necessary to change it. Keep in mind that adding more ways to do the same thing will complicate the syntax and makes it harder for everyone, to understand what is happening. |
People that knows spock will fix it quickly we have to try to bring more people into the fold, and those people are using Junit. I am migrating every single project that I find from Junit into Spock, training everyone and this bug is very confusing for new people used to Junit |
I created a new issue for that behaviour now: |
By default Spock uses a match first algorithm to determine the defined return value of a method. So whenever there are multiple defined return values, it will pick the first (that is not exhausted). This change enables a user to change this to a match last algorithm. So Spock will pick the last defined return value (that is not exhausted). This enables easy overriding of default return values. As requested in issue spockframework#26, spockframework#251, spockframework#321 and spockframework#962.
But maybe it would be nice add a for instance some flag to Stub/Mock setup:
What do You think? @leonard84 |
Originally reported on Google Code with ID 199
Reported by
frederic.pape
on 2011-09-13 11:08:13The text was updated successfully, but these errors were encountered: